@kenz1117/dsh-engram 0.11.0 → 0.11.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.
- package/CHANGELOG.md +44 -1
- package/i18n/ru.txt +3 -0
- package/icon-256.svg +38 -38
- package/icon.svg +38 -27
- package/lib/client.js +3 -3
- package/lib/client.js.map +1 -1
- package/lib/index.js +717 -337
- package/package.json +6 -2
package/lib/index.js
CHANGED
|
@@ -105,7 +105,11 @@ function appleLocaleBase() {
|
|
|
105
105
|
*
|
|
106
106
|
* Порядок чтения: переменные окружения (если в них **настоящий** язык — их
|
|
107
107
|
* ставили руками, и это более узкое намерение, чем настройка ОС), затем на macOS
|
|
108
|
-
* — `AppleLocale`,
|
|
108
|
+
* — `AppleLocale`, затем — локаль по умолчанию из ICU (`Intl`): на Windows ICU
|
|
109
|
+
* берёт её из `GetUserDefaultLocaleName`, и это единственный надёжный источник —
|
|
110
|
+
* `LC_ALL`/`LC_MESSAGES`/`LANG` там обычно не заданы вовсе (issue #39: без этой
|
|
111
|
+
* ступени китайская Windows читалась как пустая строка и падала в умолчание
|
|
112
|
+
* поставки, снося готовую 512-мерную базу на 384-мерную модель).
|
|
109
113
|
*
|
|
110
114
|
* Нужна там, где важно не «каким словарём мы располагаем», а на каком языке человек
|
|
111
115
|
* на самом деле работает: панель первого старта подставляет язык локали в выбор,
|
|
@@ -116,8 +120,25 @@ function appleLocaleBase() {
|
|
|
116
120
|
function systemLocaleBase() {
|
|
117
121
|
const fromEnv = parseLocaleTag(process.env.LC_ALL ?? process.env.LC_MESSAGES ?? process.env.LANG ?? "");
|
|
118
122
|
if (fromEnv !== "") return fromEnv;
|
|
119
|
-
if (process.platform === "darwin")
|
|
120
|
-
|
|
123
|
+
if (process.platform === "darwin") {
|
|
124
|
+
const apple = appleLocaleBase();
|
|
125
|
+
if (apple !== "") return apple;
|
|
126
|
+
}
|
|
127
|
+
return icuLocaleBase();
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Локаль по умолчанию из ICU: Node (full-icu) резолвит её из настроек ОС —
|
|
131
|
+
* на Windows это региональный формат пользователя (`GetUserDefaultLocaleName`),
|
|
132
|
+
* на Linux/macOS — соответствующие системные источники. Отличать от «локали
|
|
133
|
+
* процесса» больше не нужно: здесь это как раз источник последней надежды, когда
|
|
134
|
+
* переменные окружения пусты, а своего чтения у платформы нет.
|
|
135
|
+
*/
|
|
136
|
+
function icuLocaleBase() {
|
|
137
|
+
try {
|
|
138
|
+
return parseLocaleTag(Intl.DateTimeFormat().resolvedOptions().locale);
|
|
139
|
+
} catch {
|
|
140
|
+
return "";
|
|
141
|
+
}
|
|
121
142
|
}
|
|
122
143
|
/**
|
|
123
144
|
* Язык системы. Берётся из Intl: «ru-RU» → «ru».
|
|
@@ -205,6 +226,7 @@ const zh$5 = {
|
|
|
205
226
|
"core.warnReplayPendingFailed": "[dsh-engram] 待补做摄取重放失败(保留 pending,不影响对话):",
|
|
206
227
|
"ingest.replayKeyFailed": "[dsh-engram] 重放单条 pending 失败(保留,下个会话重试):",
|
|
207
228
|
"ingest.replayAbandoned": "[dsh-engram] 陈旧 pending 放弃重放(会话超 14 天仍失败,出队):",
|
|
229
|
+
"ingest.candidateFailed": "[dsh-engram] 单条摄取候选落库失败(跳过该条,其余候选照常):",
|
|
208
230
|
"core.warnIngestPreviousFailed": "[dsh-engram] 本轮自动摄取失败(已跳过,不影响对话):",
|
|
209
231
|
"core.warnHistoryBackfillFailed": "[dsh-engram] 历史回填失败:",
|
|
210
232
|
"core.warnDisposeFinalFailed": "[dsh-engram] 会话结束的收尾处理异常(不影响对话):",
|
|
@@ -260,6 +282,7 @@ const en$5 = {
|
|
|
260
282
|
"core.warnReplayPendingFailed": "[dsh-engram] Replaying a pending ingest failed (pending kept, conversation unaffected):",
|
|
261
283
|
"ingest.replayKeyFailed": "[dsh-engram] Replaying one pending key failed (kept for next session):",
|
|
262
284
|
"ingest.replayAbandoned": "[dsh-engram] Stale pending abandoned (session older than 14 days, still failing — dequeued):",
|
|
285
|
+
"ingest.candidateFailed": "[dsh-engram] one ingest candidate failed to write (skipped; remaining candidates proceed as usual):",
|
|
263
286
|
"core.warnIngestPreviousFailed": "[dsh-engram] Automatic ingest of this turn failed (skipped, conversation unaffected):",
|
|
264
287
|
"core.warnHistoryBackfillFailed": "[dsh-engram] History backfill failed:",
|
|
265
288
|
"core.warnDisposeFinalFailed": "[dsh-engram] Session-end finalisation threw (conversation unaffected):",
|
|
@@ -521,6 +544,8 @@ const zh$2 = {
|
|
|
521
544
|
"store.mergeTargetMissing": "write-disposition: 并入目标 {id} 不存在",
|
|
522
545
|
"store.embedderDownloadFailed": "嵌入模型下载/加载失败;检索已降级为纯关键词模式,可检查网络后重试",
|
|
523
546
|
"store.embedderDimensionMismatch": "嵌入向量维度不一致:库里是 {expected} 维,嵌入器给出 {found} 维。比较与写入直接拒绝,而不是逐维截断——请把模型改回去,或用新模型重建库中向量",
|
|
547
|
+
"store.embedderInvalidVector": "嵌入向量含非有限分量(NaN/∞),写入被拒——这样的向量落库后余弦恒不可比,整条记忆会从语义检索里静默消失",
|
|
548
|
+
"store.restoreBusyWriters": "检测到 {file} 仍有活动连接在写入(可能是另一个打开着的 DeepSeek 窗口),已取消恢复——请先关闭其他窗口再试,否则那些窗口的写入会丢",
|
|
524
549
|
"store.embedderModelUnresolved": "未能按语言确定本地嵌入模型;检索已降级为纯关键词模式,请检查模型登记表",
|
|
525
550
|
"store.embedderRemoteUnreachable": "嵌入端点不可达({baseUrl},超时 {timeoutMs} 毫秒)",
|
|
526
551
|
"store.embedderRemoteHttp": "嵌入端点返回 HTTP {status}{detail}",
|
|
@@ -566,6 +591,8 @@ const en$2 = {
|
|
|
566
591
|
"store.mergeTargetMissing": "write-disposition: merge target {id} does not exist",
|
|
567
592
|
"store.embedderDownloadFailed": "embedding model download/load failed; retrieval has degraded to keyword-only mode — check the network and retry",
|
|
568
593
|
"store.embedderDimensionMismatch": "embedding dimension mismatch: the store holds {expected}-dimensional vectors, the embedder returned {found}. Comparison and writes are refused instead of being silently truncated — switch the model back, or re-embed the store with the new one",
|
|
594
|
+
"store.embedderInvalidVector": "embedding vector contains a non-finite component (NaN/∞); the write is refused — such a vector makes cosine comparisons meaningless and the memory would silently vanish from semantic retrieval",
|
|
595
|
+
"store.restoreBusyWriters": "{file} still has a live connection writing (likely another open DeepSeek window); restore cancelled — close the other windows first, otherwise their writes would be lost",
|
|
569
596
|
"store.embedderModelUnresolved": "could not resolve a local embedding model for this language; retrieval has degraded to keyword-only mode — check the model registry",
|
|
570
597
|
"store.embedderRemoteUnreachable": "embedding endpoint unreachable ({baseUrl}, timeout {timeoutMs} ms)",
|
|
571
598
|
"store.embedderRemoteHttp": "embedding endpoint returned HTTP {status}{detail}",
|
|
@@ -578,7 +605,7 @@ const en$2 = {
|
|
|
578
605
|
//#endregion
|
|
579
606
|
//#region src/i18n/catalog/tool.ts
|
|
580
607
|
/**
|
|
581
|
-
* Словарь:
|
|
608
|
+
* Словарь: 23 инструмента `engram_*` — описания схем, тексты результатов и ошибки.
|
|
582
609
|
*
|
|
583
610
|
* Ключи этой области начинаются с `tool.`. Оригинальный китайский текст автора
|
|
584
611
|
* сохранён в `zh`; английский набор — образец полноты, русский может быть неполным
|
|
@@ -2279,6 +2306,16 @@ const en = {
|
|
|
2279
2306
|
profileDiffTarget: "Compare version",
|
|
2280
2307
|
profileDiffNone: "The two versions are identical",
|
|
2281
2308
|
profileLoadFailed: "Failed to load profile",
|
|
2309
|
+
aboutDesc: "Local-first cross-session long-term memory palace",
|
|
2310
|
+
aboutAuthor: "Author",
|
|
2311
|
+
aboutRepo: "Repository",
|
|
2312
|
+
aboutNpm: "npm",
|
|
2313
|
+
aboutLicense: "License",
|
|
2314
|
+
moreWorksTitle: "More plugins",
|
|
2315
|
+
adBillingName: "Usage Billing",
|
|
2316
|
+
adBillingDesc: "Real usage · history ledger · peak/valley pricing · subscription quota",
|
|
2317
|
+
adRainbowName: "RainbowSpeak",
|
|
2318
|
+
adRainbowDesc: "A flowing rainbow border + Xiao-Hong the window pet",
|
|
2282
2319
|
tabEpisodes: "Episodes",
|
|
2283
2320
|
episodeSince: "From date",
|
|
2284
2321
|
episodeUntil: "To date",
|
|
@@ -2407,6 +2444,7 @@ const en = {
|
|
|
2407
2444
|
corridorIndexStaying: "staying",
|
|
2408
2445
|
corridorIndexRooms: "{n} rooms",
|
|
2409
2446
|
skipEmptyAnswer: "empty output (empty after retry)",
|
|
2447
|
+
skipAllCandidatesFailed: "every candidate failed to write (kept for replay)",
|
|
2410
2448
|
tabSpiral: "Memory Spiral",
|
|
2411
2449
|
spiralAriaLabel: "memory spiral view",
|
|
2412
2450
|
spiralCenterLabel: "Now",
|
|
@@ -2839,6 +2877,16 @@ const ru = {
|
|
|
2839
2877
|
profileDiffTarget: "Версия для сравнения",
|
|
2840
2878
|
profileDiffNone: "Версии идентичны",
|
|
2841
2879
|
profileLoadFailed: "Не удалось загрузить профиль",
|
|
2880
|
+
aboutDesc: "Локальная межсессионная долговременная память · дворец памяти",
|
|
2881
|
+
aboutAuthor: "Автор",
|
|
2882
|
+
aboutRepo: "Репозиторий",
|
|
2883
|
+
aboutNpm: "npm",
|
|
2884
|
+
aboutLicense: "Лицензия",
|
|
2885
|
+
moreWorksTitle: "Другие плагины",
|
|
2886
|
+
adBillingName: "Usage Billing",
|
|
2887
|
+
adBillingDesc: "Реальное использование · история · тарифы пик/долина · квоты подписки",
|
|
2888
|
+
adRainbowName: "RainbowSpeak",
|
|
2889
|
+
adRainbowDesc: "Переливающаяся радужная рамка + питомец «Сяохун»",
|
|
2842
2890
|
tabEpisodes: "Эпизоды",
|
|
2843
2891
|
episodeSince: "С даты",
|
|
2844
2892
|
episodeUntil: "По дату",
|
|
@@ -2967,6 +3015,7 @@ const ru = {
|
|
|
2967
3015
|
corridorIndexStaying: "хранится",
|
|
2968
3016
|
corridorIndexRooms: "комнат: {n}",
|
|
2969
3017
|
skipEmptyAnswer: "вывод дистилляции пуст (пуст и после повтора)",
|
|
3018
|
+
skipAllCandidatesFailed: "все кандидаты не записались (сохранено для повтора)",
|
|
2970
3019
|
tabSpiral: "Спираль воспоминаний",
|
|
2971
3020
|
spiralAriaLabel: "спираль воспоминаний",
|
|
2972
3021
|
spiralCenterLabel: "сейчас",
|
|
@@ -3397,6 +3446,16 @@ const zh = {
|
|
|
3397
3446
|
profileDiffTarget: "对比版本",
|
|
3398
3447
|
profileDiffNone: "两个版本内容相同",
|
|
3399
3448
|
profileLoadFailed: "画像加载失败",
|
|
3449
|
+
aboutDesc: "本地优先的跨会话长期记忆 · 记忆宫殿",
|
|
3450
|
+
aboutAuthor: "作者",
|
|
3451
|
+
aboutRepo: "仓库",
|
|
3452
|
+
aboutNpm: "npm",
|
|
3453
|
+
aboutLicense: "许可证",
|
|
3454
|
+
moreWorksTitle: "更多作品",
|
|
3455
|
+
adBillingName: "Usage Billing · 用量账单",
|
|
3456
|
+
adBillingDesc: "真实用量 · 历史账本 · 峰谷计价 · 订阅额度",
|
|
3457
|
+
adRainbowName: "RainbowSpeak · 虹语",
|
|
3458
|
+
adRainbowDesc: "会流动的彩虹边框 + 窗口桌宠「小虹」",
|
|
3400
3459
|
tabEpisodes: "往事",
|
|
3401
3460
|
episodeSince: "开始日期",
|
|
3402
3461
|
episodeUntil: "结束日期",
|
|
@@ -3525,6 +3584,7 @@ const zh = {
|
|
|
3525
3584
|
corridorIndexStaying: "条驻留",
|
|
3526
3585
|
corridorIndexRooms: "{n} 间房",
|
|
3527
3586
|
skipEmptyAnswer: "提炼输出为空(重试后仍空)",
|
|
3587
|
+
skipAllCandidatesFailed: "全部候选落库失败(保留待重放)",
|
|
3528
3588
|
tabSpiral: "回忆螺旋",
|
|
3529
3589
|
spiralAriaLabel: "回忆螺旋视图",
|
|
3530
3590
|
spiralCenterLabel: "现在",
|
|
@@ -4297,7 +4357,9 @@ function parseFillAnswer(text, terms) {
|
|
|
4297
4357
|
/** Список переводов по порядку запроса: модель отвечает и так, без левых строк. */
|
|
4298
4358
|
const byOrder = (list) => {
|
|
4299
4359
|
if (list.length !== terms.length || !list.every((item) => typeof item === "string")) return;
|
|
4300
|
-
list.forEach((item, index) =>
|
|
4360
|
+
list.forEach((item, index) => {
|
|
4361
|
+
accept(terms[index], item);
|
|
4362
|
+
});
|
|
4301
4363
|
};
|
|
4302
4364
|
/** Карта: ключ — английская строка (или обёртка с массивом переводов внутри). */
|
|
4303
4365
|
const fromRecord = (record, depth) => {
|
|
@@ -5713,10 +5775,13 @@ function resolveBaseTarget(registry, language, embedder) {
|
|
|
5713
5775
|
* Построить эмбеддер по конфигурации и реестру.
|
|
5714
5776
|
* @param config - разрешённая конфигурация плагина.
|
|
5715
5777
|
* @param registry - прочитанные каталог моделей и привязки.
|
|
5778
|
+
* @param languageOverride - язык выбора локальной модели вместо `config.language`
|
|
5779
|
+
* (issue #39: метка существующей базы главнее системной локали — locale читается
|
|
5780
|
+
* не на всех платформах, а промах уводит готовую базу на чужую размерность).
|
|
5716
5781
|
* @returns готовый EngramEmbedder.
|
|
5717
5782
|
* @throws EngramError EMBEDDER_MODEL_UNRESOLVED — язык не привязан ни к одной модели; EMBEDDER_DOWNLOAD_FAILED / EMBEDDER_REMOTE_FAILED — бэкенд не поднялся.
|
|
5718
5783
|
*/
|
|
5719
|
-
function createEmbedder(config, registry) {
|
|
5784
|
+
function createEmbedder(config, registry, languageOverride) {
|
|
5720
5785
|
const embedder = config.embedder;
|
|
5721
5786
|
const remote = embedder.remote;
|
|
5722
5787
|
if (remote !== void 0) return createRemoteEmbedder({
|
|
@@ -5727,7 +5792,7 @@ function createEmbedder(config, registry) {
|
|
|
5727
5792
|
batchSize: remote.batchSize,
|
|
5728
5793
|
timeoutMs: remote.timeoutMs
|
|
5729
5794
|
});
|
|
5730
|
-
const spec = resolveLocalModel(registry, config.language, embedder.localModel, embedder.localPooling);
|
|
5795
|
+
const spec = resolveLocalModel(registry, languageOverride ?? config.language, embedder.localModel, embedder.localPooling);
|
|
5731
5796
|
if (spec === void 0) return Promise.reject(new EngramError("EMBEDDER_MODEL_UNRESOLVED", message("store.embedderModelUnresolved")));
|
|
5732
5797
|
return createLocalEmbedder(config.modelCacheDir, config.hfEndpoint, spec);
|
|
5733
5798
|
}
|
|
@@ -6110,7 +6175,7 @@ async function resolveBand(judge, neighbor, similarity, kind, content, store) {
|
|
|
6110
6175
|
const p = (await judge.ask(`Memory A:\n${neighbor.content}\n\nMemory B:\n${content}`, [{
|
|
6111
6176
|
id: "same_memory",
|
|
6112
6177
|
instructions: `Are Memory A and Memory B the same fact, i.e. restatements of one identical statement (both are "${kind}" memories)? Answer true only for genuine restatements.`
|
|
6113
|
-
}]))
|
|
6178
|
+
}])).same_memory;
|
|
6114
6179
|
recordJevObservation({
|
|
6115
6180
|
at: startedAt,
|
|
6116
6181
|
site: "band",
|
|
@@ -6203,7 +6268,7 @@ async function confirmContradictions(judge, incoming, candidates, store) {
|
|
|
6203
6268
|
const p = (await judge.ask(`Memory A:\n${candidate.content}\n\nMemory B:\n${incoming}`, [{
|
|
6204
6269
|
id: "contradicts",
|
|
6205
6270
|
instructions: "Do Memory A and Memory B state facts that cannot both be true at the same time (a genuine contradiction)?"
|
|
6206
|
-
}]))
|
|
6271
|
+
}])).contradicts;
|
|
6207
6272
|
recordJevObservation({
|
|
6208
6273
|
at: startedAt,
|
|
6209
6274
|
site: "contradiction",
|
|
@@ -6280,6 +6345,13 @@ const INGEST_MAX_TOKENS = 4e3;
|
|
|
6280
6345
|
const INGEST_DONE_OP = "ingest-done";
|
|
6281
6346
|
/** op_log pending 键 op:disposed 末轮摄取失败/超时,待下次会话重放补做。 */
|
|
6282
6347
|
const INGEST_PENDING_OP = "ingest-pending";
|
|
6348
|
+
/**
|
|
6349
|
+
* op_log 跳过标记 op:该 (sessionId, turn) 被确定性的前置检查跳过(节流/无正文/无路由)。
|
|
6350
|
+
* 不记它,估算与重跑就永远把这些轮当待处理(issue #40:回填的「待处理」降不到零)。
|
|
6351
|
+
* 跳过判定由轮内容决定、逐字可复现,标记因此不掩盖内容;target_id 存原因
|
|
6352
|
+
* (turn key 已含 sessionId,用它携带原因供日后按规则版本清理),detail 与 done 同键。
|
|
6353
|
+
*/
|
|
6354
|
+
const INGEST_SKIP_OP = "ingest-skip";
|
|
6283
6355
|
/** disposed 末轮摄取的超时(fire-and-forget 观察器,进程退出可能打断,必须限时)。 */
|
|
6284
6356
|
const FINAL_INGEST_TIMEOUT_MS = 5e3;
|
|
6285
6357
|
/** 幂等键编码:`${sessionId}#${turn}`。 */
|
|
@@ -6581,40 +6653,58 @@ async function ingestPreviousTurn(deps) {
|
|
|
6581
6653
|
dropped: 0,
|
|
6582
6654
|
skipped: "already-ingested"
|
|
6583
6655
|
};
|
|
6656
|
+
if (await auditStore.hasAudit("ingest-skip", doneKey)) return {
|
|
6657
|
+
scannedEvents: slice.length,
|
|
6658
|
+
candidates: 0,
|
|
6659
|
+
written: 0,
|
|
6660
|
+
merged: 0,
|
|
6661
|
+
deferred: 0,
|
|
6662
|
+
dropped: 0,
|
|
6663
|
+
skipped: "already-ingested"
|
|
6664
|
+
};
|
|
6584
6665
|
if (sliceMode === "previous" || deps.throttle === true) {
|
|
6585
6666
|
const throttled = throttleDecision(slice);
|
|
6586
|
-
if (throttled !== null)
|
|
6667
|
+
if (throttled !== null) {
|
|
6668
|
+
await auditStore.audit(INGEST_SKIP_OP, throttled, doneKey);
|
|
6669
|
+
return {
|
|
6670
|
+
scannedEvents: slice.length,
|
|
6671
|
+
candidates: 0,
|
|
6672
|
+
written: 0,
|
|
6673
|
+
merged: 0,
|
|
6674
|
+
deferred: 0,
|
|
6675
|
+
dropped: 0,
|
|
6676
|
+
skipped: throttled
|
|
6677
|
+
};
|
|
6678
|
+
}
|
|
6679
|
+
}
|
|
6680
|
+
const scoped = omitRecallToolResults(slice);
|
|
6681
|
+
const { texts, minSeq } = collectTexts(scoped, limits.includeAssistant);
|
|
6682
|
+
const cleaned = texts.map((text) => redactSecrets(sanitizeProtocolText(text)));
|
|
6683
|
+
if (cleaned.every((text) => text === "")) {
|
|
6684
|
+
await auditStore.audit(INGEST_SKIP_OP, "no-user-content", doneKey);
|
|
6685
|
+
return {
|
|
6587
6686
|
scannedEvents: slice.length,
|
|
6588
6687
|
candidates: 0,
|
|
6589
6688
|
written: 0,
|
|
6590
6689
|
merged: 0,
|
|
6591
6690
|
deferred: 0,
|
|
6592
6691
|
dropped: 0,
|
|
6593
|
-
skipped:
|
|
6692
|
+
skipped: "no-user-content"
|
|
6594
6693
|
};
|
|
6595
6694
|
}
|
|
6596
|
-
const scoped = omitRecallToolResults(slice);
|
|
6597
|
-
const { texts, minSeq } = collectTexts(scoped, limits.includeAssistant);
|
|
6598
|
-
const cleaned = texts.map((text) => redactSecrets(sanitizeProtocolText(text)));
|
|
6599
|
-
if (cleaned.every((text) => text === "")) return {
|
|
6600
|
-
scannedEvents: slice.length,
|
|
6601
|
-
candidates: 0,
|
|
6602
|
-
written: 0,
|
|
6603
|
-
merged: 0,
|
|
6604
|
-
deferred: 0,
|
|
6605
|
-
dropped: 0,
|
|
6606
|
-
skipped: "no-user-content"
|
|
6607
|
-
};
|
|
6608
6695
|
const route = deps.routeOverride ?? routeFromEvents(deps.events);
|
|
6609
|
-
if (route === void 0)
|
|
6610
|
-
|
|
6611
|
-
|
|
6612
|
-
|
|
6613
|
-
|
|
6614
|
-
|
|
6615
|
-
|
|
6616
|
-
|
|
6617
|
-
|
|
6696
|
+
if (route === void 0) {
|
|
6697
|
+
await auditStore.audit(INGEST_SKIP_OP, "no-route-in-log", doneKey);
|
|
6698
|
+
return {
|
|
6699
|
+
scannedEvents: slice.length,
|
|
6700
|
+
candidates: 0,
|
|
6701
|
+
written: 0,
|
|
6702
|
+
merged: 0,
|
|
6703
|
+
deferred: 0,
|
|
6704
|
+
dropped: 0,
|
|
6705
|
+
skipped: "no-route-in-log"
|
|
6706
|
+
};
|
|
6707
|
+
}
|
|
6618
6708
|
const recallNote = hasRecallToolCalls(scoped) ? message("llm.ingestRecallNote") : "";
|
|
6619
6709
|
const userText = `${message("llm.ingestUserPrompt")}\n${JSON.stringify(cleaned)}${recallNote === "" ? "" : `\n${recallNote}`}`;
|
|
6620
6710
|
deps.logRequest({
|
|
@@ -6660,6 +6750,7 @@ async function ingestPreviousTurn(deps) {
|
|
|
6660
6750
|
let merged = 0;
|
|
6661
6751
|
let deferred = 0;
|
|
6662
6752
|
let dropped = 0;
|
|
6753
|
+
let failedDrops = 0;
|
|
6663
6754
|
for (const item of parsed.slice(0, limits.maxCandidates)) {
|
|
6664
6755
|
const candidate = item;
|
|
6665
6756
|
if (typeof candidate.content !== "string" || candidate.content.trim() === "") {
|
|
@@ -6680,68 +6771,83 @@ async function ingestPreviousTurn(deps) {
|
|
|
6680
6771
|
].includes(candidate.kind) ? candidate.kind : "fact";
|
|
6681
6772
|
const modelScope = candidate.scope === "user" || candidate.scope === "project" ? candidate.scope : void 0;
|
|
6682
6773
|
const scope = deps.perCandidateScope === true && modelScope !== void 0 ? modelScope : deps.writeScope ?? "user";
|
|
6683
|
-
const target = deps.resolveStore === void 0 ? store : await deps.resolveStore(scope);
|
|
6684
6774
|
const importance = typeof candidate.importance === "number" && Number.isFinite(candidate.importance) ? Math.min(1, Math.max(0, candidate.importance)) : .5;
|
|
6685
|
-
const linkEntities = async (nodeId) => {
|
|
6686
|
-
const mentions = parseEntityMentions(candidate.entities);
|
|
6687
|
-
if (mentions.length === 0) return;
|
|
6688
|
-
try {
|
|
6689
|
-
const entities = await target.resolveEntities(mentions);
|
|
6690
|
-
await target.linkNodeEntities(nodeId, entities.map((entity) => entity.id));
|
|
6691
|
-
const factInputs = [];
|
|
6692
|
-
const byName = new Map(entities.map((entity) => [normalizeEntityName(entity.name), entity]));
|
|
6693
|
-
for (const mention of parseFactMentions(candidate.facts)) {
|
|
6694
|
-
const entity = byName.get(normalizeEntityName(mention.entity));
|
|
6695
|
-
if (entity === void 0) continue;
|
|
6696
|
-
factInputs.push({
|
|
6697
|
-
entityId: entity.id,
|
|
6698
|
-
content: mention.content,
|
|
6699
|
-
sourceNodeId: nodeId
|
|
6700
|
-
});
|
|
6701
|
-
}
|
|
6702
|
-
if (factInputs.length > 0) await target.writeFacts(factInputs);
|
|
6703
|
-
} catch {}
|
|
6704
|
-
};
|
|
6705
6775
|
if (writtenContents.includes(content)) {
|
|
6706
6776
|
dropped += 1;
|
|
6707
6777
|
continue;
|
|
6708
6778
|
}
|
|
6709
|
-
|
|
6710
|
-
|
|
6711
|
-
|
|
6712
|
-
|
|
6713
|
-
|
|
6714
|
-
|
|
6715
|
-
|
|
6716
|
-
|
|
6717
|
-
|
|
6718
|
-
|
|
6719
|
-
|
|
6720
|
-
|
|
6721
|
-
|
|
6722
|
-
|
|
6723
|
-
|
|
6724
|
-
|
|
6725
|
-
|
|
6726
|
-
|
|
6727
|
-
|
|
6728
|
-
|
|
6729
|
-
|
|
6730
|
-
|
|
6731
|
-
|
|
6732
|
-
|
|
6733
|
-
|
|
6734
|
-
|
|
6735
|
-
|
|
6736
|
-
|
|
6737
|
-
await target
|
|
6779
|
+
try {
|
|
6780
|
+
const target = deps.resolveStore === void 0 ? store : await deps.resolveStore(scope);
|
|
6781
|
+
const linkEntities = async (nodeId) => {
|
|
6782
|
+
const mentions = parseEntityMentions(candidate.entities);
|
|
6783
|
+
if (mentions.length === 0) return;
|
|
6784
|
+
try {
|
|
6785
|
+
const entities = await target.resolveEntities(mentions);
|
|
6786
|
+
await target.linkNodeEntities(nodeId, entities.map((entity) => entity.id));
|
|
6787
|
+
const factInputs = [];
|
|
6788
|
+
const byName = new Map(entities.map((entity) => [normalizeEntityName(entity.name), entity]));
|
|
6789
|
+
for (const mention of parseFactMentions(candidate.facts)) {
|
|
6790
|
+
const entity = byName.get(normalizeEntityName(mention.entity));
|
|
6791
|
+
if (entity === void 0) continue;
|
|
6792
|
+
factInputs.push({
|
|
6793
|
+
entityId: entity.id,
|
|
6794
|
+
content: mention.content,
|
|
6795
|
+
sourceNodeId: nodeId
|
|
6796
|
+
});
|
|
6797
|
+
}
|
|
6798
|
+
if (factInputs.length > 0) await target.writeFacts(factInputs);
|
|
6799
|
+
} catch {}
|
|
6800
|
+
};
|
|
6801
|
+
const vector = embedder === void 0 ? void 0 : (await embedder.embed([content]))[0];
|
|
6802
|
+
const decision = await decideWrite(target, kind, vector, judge === void 0 ? {} : {
|
|
6803
|
+
judge,
|
|
6804
|
+
content
|
|
6805
|
+
});
|
|
6806
|
+
if (decision.disposition === "merge") {
|
|
6807
|
+
await applyMerge(target, decision, content);
|
|
6808
|
+
await linkEntities(decision.into.id);
|
|
6809
|
+
writtenContents.push(content);
|
|
6810
|
+
merged += 1;
|
|
6811
|
+
continue;
|
|
6738
6812
|
}
|
|
6739
|
-
|
|
6813
|
+
const record = await target.write({
|
|
6814
|
+
scope,
|
|
6815
|
+
kind,
|
|
6816
|
+
content,
|
|
6817
|
+
importance,
|
|
6818
|
+
confidence: limits.confidence,
|
|
6819
|
+
sourceSessionId: deps.sessionId,
|
|
6820
|
+
sourceRound: round,
|
|
6821
|
+
...minSeq === null ? {} : { sourceSeq: minSeq },
|
|
6822
|
+
...vector === void 0 ? {} : { embedding: vector },
|
|
6823
|
+
...deps.history === true ? { initialReviewAt: null } : {}
|
|
6824
|
+
});
|
|
6825
|
+
if (decision.disposition === "defer") {
|
|
6826
|
+
const neighbors = judge === void 0 ? [decision.neighbor] : await confirmContradictions(judge, content, [decision.neighbor], target) ?? [decision.neighbor];
|
|
6827
|
+
for (const neighbor of neighbors) {
|
|
6828
|
+
if (neighbor.id === record.id) continue;
|
|
6829
|
+
await target.linkEdge(record.id, neighbor.id, "contradicts");
|
|
6830
|
+
}
|
|
6831
|
+
deferred += 1;
|
|
6832
|
+
}
|
|
6833
|
+
await linkEntities(record.id);
|
|
6834
|
+
writtenContents.push(content);
|
|
6835
|
+
written += 1;
|
|
6836
|
+
} catch (error) {
|
|
6837
|
+
dropped += 1;
|
|
6838
|
+
failedDrops += 1;
|
|
6839
|
+
console.warn(message("ingest.candidateFailed"), content.slice(0, 80), error);
|
|
6740
6840
|
}
|
|
6741
|
-
await linkEntities(record.id);
|
|
6742
|
-
writtenContents.push(content);
|
|
6743
|
-
written += 1;
|
|
6744
6841
|
}
|
|
6842
|
+
if (failedDrops > 0 && written === 0 && merged === 0 && deferred === 0) return {
|
|
6843
|
+
scannedEvents: slice.length,
|
|
6844
|
+
candidates: parsed.length,
|
|
6845
|
+
written,
|
|
6846
|
+
merged,
|
|
6847
|
+
deferred,
|
|
6848
|
+
dropped,
|
|
6849
|
+
skipped: "all-candidates-failed"
|
|
6850
|
+
};
|
|
6745
6851
|
await auditStore.audit(INGEST_DONE_OP, deps.sessionId, doneKey);
|
|
6746
6852
|
return {
|
|
6747
6853
|
scannedEvents: slice.length,
|
|
@@ -6899,7 +7005,7 @@ async function replayPendingIngests(deps) {
|
|
|
6899
7005
|
...deps.resolveJudge === void 0 ? {} : { resolveJudge: deps.resolveJudge },
|
|
6900
7006
|
signal: deps.signal
|
|
6901
7007
|
});
|
|
6902
|
-
if (outcome.skipped === "unparseable-output" || outcome.skipped === "empty-answer") {
|
|
7008
|
+
if (outcome.skipped === "unparseable-output" || outcome.skipped === "empty-answer" || outcome.skipped === "all-candidates-failed") {
|
|
6903
7009
|
kept += 1;
|
|
6904
7010
|
if (stale) {
|
|
6905
7011
|
await store.clearAudit(INGEST_PENDING_OP, detail);
|
|
@@ -7112,7 +7218,11 @@ async function estimateHistoryBackfill(deps, defaults, overrides = {}) {
|
|
|
7112
7218
|
for (const candidate of candidates) {
|
|
7113
7219
|
eligibleTurns += candidate.turns.length;
|
|
7114
7220
|
if (auditStore === void 0) continue;
|
|
7115
|
-
for (const turn of candidate.turns)
|
|
7221
|
+
for (const turn of candidate.turns) {
|
|
7222
|
+
const key = encodeTurnKey(candidate.header.id, turn);
|
|
7223
|
+
if (await auditStore.hasAudit("ingest-done", key)) alreadyIngested += 1;
|
|
7224
|
+
else if (await auditStore.hasAudit("ingest-skip", key)) alreadyIngested += 1;
|
|
7225
|
+
}
|
|
7116
7226
|
}
|
|
7117
7227
|
return {
|
|
7118
7228
|
rules,
|
|
@@ -7280,6 +7390,10 @@ const DEFAULTS = {
|
|
|
7280
7390
|
importanceBelow: .3,
|
|
7281
7391
|
mergeThreshold: .92
|
|
7282
7392
|
};
|
|
7393
|
+
/** 合并阶段的工作集封顶:超出交给归档/衰减处置(审计 N12——两两比对必须设界)。 */
|
|
7394
|
+
const CONSOLIDATION_MAX_RECORDS = 500;
|
|
7395
|
+
/** 批式嵌入的块大小(一次 ONNX 前向的条数)。 */
|
|
7396
|
+
const EMBED_BATCH = 32;
|
|
7283
7397
|
/** 闭馆整理主函数:纯异步,不抛错(失败仅记日志 / 落 pending)。 */
|
|
7284
7398
|
async function runConsolidation(store, embedder, opts = {}) {
|
|
7285
7399
|
const start = Date.now();
|
|
@@ -7288,35 +7402,27 @@ async function runConsolidation(store, embedder, opts = {}) {
|
|
|
7288
7402
|
...opts
|
|
7289
7403
|
};
|
|
7290
7404
|
const scope = options.scope ?? "user";
|
|
7291
|
-
const stats = await store.stats();
|
|
7292
|
-
const records = await store.topActive(scope, Math.max(stats.active, 0));
|
|
7293
|
-
const archivedIds = /* @__PURE__ */ new Set();
|
|
7294
7405
|
let archived = 0;
|
|
7295
7406
|
let merged = 0;
|
|
7296
7407
|
let skipped = 0;
|
|
7297
|
-
|
|
7298
|
-
|
|
7299
|
-
|
|
7300
|
-
|
|
7301
|
-
|
|
7302
|
-
|
|
7303
|
-
|
|
7304
|
-
await store.forget(record.id);
|
|
7305
|
-
archivedIds.add(record.id);
|
|
7306
|
-
archived += 1;
|
|
7307
|
-
} catch {
|
|
7308
|
-
skipped += 1;
|
|
7309
|
-
}
|
|
7408
|
+
try {
|
|
7409
|
+
archived = await store.decay({
|
|
7410
|
+
importanceBelow: options.importanceBelow,
|
|
7411
|
+
olderThanDays: options.olderThanDays
|
|
7412
|
+
});
|
|
7413
|
+
} catch {
|
|
7414
|
+
skipped += 1;
|
|
7310
7415
|
}
|
|
7416
|
+
const seedIds = options.mergeCandidateIds !== void 0 ? new Set(options.mergeCandidateIds) : null;
|
|
7417
|
+
const stats = await store.stats();
|
|
7418
|
+
const records = await store.topActive(scope, seedIds === null ? Math.min(Math.max(stats.active, 0), CONSOLIDATION_MAX_RECORDS) : Math.max(stats.active, 0));
|
|
7311
7419
|
const emb = await embedder;
|
|
7312
7420
|
const buckets = /* @__PURE__ */ new Map();
|
|
7313
7421
|
for (const record of records) {
|
|
7314
|
-
if (archivedIds.has(record.id)) continue;
|
|
7315
7422
|
const list = buckets.get(record.kind) ?? [];
|
|
7316
7423
|
list.push(record);
|
|
7317
7424
|
buckets.set(record.kind, list);
|
|
7318
7425
|
}
|
|
7319
|
-
const seedIds = options.mergeCandidateIds !== void 0 ? new Set(options.mergeCandidateIds) : null;
|
|
7320
7426
|
for (const list of buckets.values()) {
|
|
7321
7427
|
if (list.length < 2) continue;
|
|
7322
7428
|
if (seedIds !== null && !list.some((record) => seedIds.has(record.id))) continue;
|
|
@@ -7343,13 +7449,22 @@ async function runConsolidation(store, embedder, opts = {}) {
|
|
|
7343
7449
|
continue;
|
|
7344
7450
|
}
|
|
7345
7451
|
const vectors = /* @__PURE__ */ new Map();
|
|
7346
|
-
|
|
7347
|
-
|
|
7452
|
+
const seedRecords = list.filter((record) => seedIds === null || seedIds.has(record.id));
|
|
7453
|
+
for (let offset = 0; offset < seedRecords.length; offset += EMBED_BATCH) {
|
|
7454
|
+
const chunk = seedRecords.slice(offset, offset + EMBED_BATCH);
|
|
7348
7455
|
try {
|
|
7349
|
-
const
|
|
7350
|
-
|
|
7456
|
+
const vecs = await emb.embed(chunk.map((record) => record.content));
|
|
7457
|
+
chunk.forEach((record, index) => {
|
|
7458
|
+
const vec = vecs[index];
|
|
7459
|
+
if (vec !== void 0) vectors.set(record.id, vec);
|
|
7460
|
+
});
|
|
7351
7461
|
} catch {
|
|
7352
|
-
|
|
7462
|
+
for (const record of chunk) try {
|
|
7463
|
+
const vec = (await emb.embed([record.content]))[0];
|
|
7464
|
+
if (vec !== void 0) vectors.set(record.id, vec);
|
|
7465
|
+
} catch {
|
|
7466
|
+
skipped += 1;
|
|
7467
|
+
}
|
|
7353
7468
|
}
|
|
7354
7469
|
}
|
|
7355
7470
|
const arr = list.filter((record) => vectors.has(record.id));
|
|
@@ -7361,6 +7476,7 @@ async function runConsolidation(store, embedder, opts = {}) {
|
|
|
7361
7476
|
for (let j = i + 1; j < list.length; j += 1) {
|
|
7362
7477
|
const b = list[j];
|
|
7363
7478
|
if (visited.has(b.id)) continue;
|
|
7479
|
+
if (b.id === a.id) continue;
|
|
7364
7480
|
const bVec = vectors.get(b.id);
|
|
7365
7481
|
if (bVec === void 0) continue;
|
|
7366
7482
|
if (cosine(vectors.get(a.id), bVec) >= options.mergeThreshold) dupes.push(b);
|
|
@@ -8132,7 +8248,7 @@ async function testJevConnection(connection) {
|
|
|
8132
8248
|
return {
|
|
8133
8249
|
ok: true,
|
|
8134
8250
|
elapsedMs: Date.now() - startedAt,
|
|
8135
|
-
probability: answers
|
|
8251
|
+
probability: answers.ping,
|
|
8136
8252
|
error: void 0,
|
|
8137
8253
|
status: 200,
|
|
8138
8254
|
kind: void 0
|
|
@@ -8193,7 +8309,7 @@ function readPath(root, path) {
|
|
|
8193
8309
|
}
|
|
8194
8310
|
/** Модели из конфигурации провайдера: `[{id, name}]`; битые записи пропускаем. */
|
|
8195
8311
|
function readModels(profile) {
|
|
8196
|
-
const models = profile?.
|
|
8312
|
+
const models = profile?.models;
|
|
8197
8313
|
if (!Array.isArray(models)) return [];
|
|
8198
8314
|
const result = [];
|
|
8199
8315
|
for (const entry of models) {
|
|
@@ -8477,11 +8593,11 @@ function collectOverride(value) {
|
|
|
8477
8593
|
const legacy = legacyJevFields(raw);
|
|
8478
8594
|
if (legacy.length > 0) noteLegacyJevFields("jev-config.json", legacy);
|
|
8479
8595
|
return {
|
|
8480
|
-
...typeof raw
|
|
8481
|
-
...typeof raw
|
|
8482
|
-
...typeof raw
|
|
8483
|
-
...typeof raw
|
|
8484
|
-
...typeof raw
|
|
8596
|
+
...typeof raw.enabled === "boolean" ? { enabled: raw.enabled } : {},
|
|
8597
|
+
...typeof raw.provider === "string" && raw.provider !== "" ? { provider: raw.provider } : {},
|
|
8598
|
+
...typeof raw.model === "string" && raw.model !== "" ? { model: raw.model } : {},
|
|
8599
|
+
...typeof raw.endpoint === "string" && raw.endpoint !== "" ? { endpoint: raw.endpoint } : {},
|
|
8600
|
+
...typeof raw.timeoutMs === "number" && Number.isInteger(raw.timeoutMs) ? { timeoutMs: raw.timeoutMs } : {}
|
|
8485
8601
|
};
|
|
8486
8602
|
}
|
|
8487
8603
|
/** Записать перекрытие целиком (вызывающий уже проверил его через `parseJevPatch`). */
|
|
@@ -8498,25 +8614,25 @@ function saveJevOverride(dbDir, override) {
|
|
|
8498
8614
|
*/
|
|
8499
8615
|
function parseJevPatch(current, patch) {
|
|
8500
8616
|
const merged = { ...current };
|
|
8501
|
-
if (patch
|
|
8502
|
-
if (typeof patch
|
|
8503
|
-
merged.enabled = patch
|
|
8617
|
+
if (patch.enabled !== void 0) {
|
|
8618
|
+
if (typeof patch.enabled !== "boolean") return message("misc.jevEnabledMustBeBoolean");
|
|
8619
|
+
merged.enabled = patch.enabled;
|
|
8504
8620
|
}
|
|
8505
|
-
if (patch
|
|
8506
|
-
if (typeof patch
|
|
8507
|
-
const trimmed = patch
|
|
8621
|
+
if (patch.provider !== void 0) {
|
|
8622
|
+
if (typeof patch.provider !== "string") return message("misc.jevProviderMustBeString");
|
|
8623
|
+
const trimmed = patch.provider.trim();
|
|
8508
8624
|
if (trimmed === "") delete merged.provider;
|
|
8509
8625
|
else merged.provider = trimmed;
|
|
8510
8626
|
}
|
|
8511
|
-
if (patch
|
|
8512
|
-
if (typeof patch
|
|
8513
|
-
const trimmed = patch
|
|
8627
|
+
if (patch.model !== void 0) {
|
|
8628
|
+
if (typeof patch.model !== "string") return message("misc.jevModelMustNotBeEmpty");
|
|
8629
|
+
const trimmed = patch.model.trim();
|
|
8514
8630
|
if (trimmed === "") delete merged.model;
|
|
8515
8631
|
else merged.model = trimmed;
|
|
8516
8632
|
}
|
|
8517
|
-
if (patch
|
|
8518
|
-
if (typeof patch
|
|
8519
|
-
const trimmed = patch
|
|
8633
|
+
if (patch.endpoint !== void 0) {
|
|
8634
|
+
if (typeof patch.endpoint !== "string") return message("misc.jevEndpointMustBeHttp");
|
|
8635
|
+
const trimmed = patch.endpoint.trim();
|
|
8520
8636
|
if (trimmed === "") delete merged.endpoint;
|
|
8521
8637
|
else {
|
|
8522
8638
|
let scheme = "";
|
|
@@ -8529,8 +8645,8 @@ function parseJevPatch(current, patch) {
|
|
|
8529
8645
|
merged.endpoint = trimmed;
|
|
8530
8646
|
}
|
|
8531
8647
|
}
|
|
8532
|
-
if (patch
|
|
8533
|
-
const value = patch
|
|
8648
|
+
if (patch.timeoutMs !== void 0) {
|
|
8649
|
+
const value = patch.timeoutMs;
|
|
8534
8650
|
if (typeof value !== "number" || !Number.isInteger(value) || value < 1e3 || value > 6e4) return message("misc.jevTimeoutMsMustBeInteger");
|
|
8535
8651
|
merged.timeoutMs = value;
|
|
8536
8652
|
}
|
|
@@ -8674,7 +8790,7 @@ function renderMemory(record, edges) {
|
|
|
8674
8790
|
const imagery = record.imagery;
|
|
8675
8791
|
const caption = imagery?.caption;
|
|
8676
8792
|
const sensory = imagery?.sensoryTags ?? [];
|
|
8677
|
-
|
|
8793
|
+
return `${[
|
|
8678
8794
|
"---",
|
|
8679
8795
|
`id: ${record.id}`,
|
|
8680
8796
|
`scope: ${record.scope}`,
|
|
@@ -8690,8 +8806,7 @@ function renderMemory(record, edges) {
|
|
|
8690
8806
|
...caption !== void 0 && caption !== null ? [`imageryCaption: ${yaml(caption)}`] : [],
|
|
8691
8807
|
...sensory.length > 0 ? [`imagerySensory: [${sensory.map((s) => yaml(s)).join(", ")}]`, `imageryValence: ${(imagery?.emotionalValence ?? 0).toFixed(3)}`] : [],
|
|
8692
8808
|
"---"
|
|
8693
|
-
].join("\n")
|
|
8694
|
-
const body = [
|
|
8809
|
+
].join("\n")}\n${[
|
|
8695
8810
|
`# ${record.content.split("\n")[0]?.slice(0, 80) ?? message("misc.mirrorUntitled")}`,
|
|
8696
8811
|
"",
|
|
8697
8812
|
record.content,
|
|
@@ -8707,8 +8822,7 @@ function renderMemory(record, edges) {
|
|
|
8707
8822
|
});
|
|
8708
8823
|
}),
|
|
8709
8824
|
""
|
|
8710
|
-
].join("\n")
|
|
8711
|
-
return frontmatter + "\n" + body;
|
|
8825
|
+
].join("\n")}`;
|
|
8712
8826
|
}
|
|
8713
8827
|
/** 房间清单 _meta.json 模板。 */
|
|
8714
8828
|
function renderMeta(scope, data, rooms) {
|
|
@@ -8718,7 +8832,7 @@ function renderMeta(scope, data, rooms) {
|
|
|
8718
8832
|
total: data.records.length,
|
|
8719
8833
|
rooms
|
|
8720
8834
|
};
|
|
8721
|
-
return JSON.stringify(payload, null, 2)
|
|
8835
|
+
return `${JSON.stringify(payload, null, 2)}\n`;
|
|
8722
8836
|
}
|
|
8723
8837
|
/** 计算房间摘要(按 kind 分组统计 active/archived/forgotten 的记忆条数)。 */
|
|
8724
8838
|
function summarizeRooms(records) {
|
|
@@ -8780,7 +8894,7 @@ async function writeMirror(rootDir, data, language) {
|
|
|
8780
8894
|
fileCount += 2;
|
|
8781
8895
|
if (scope === "shared") {
|
|
8782
8896
|
const manifest = buildShareManifest(data);
|
|
8783
|
-
await writeFile(join(rootDir, "share-manifest.json"), JSON.stringify(manifest, null, 2)
|
|
8897
|
+
await writeFile(join(rootDir, "share-manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`, { mode: 384 });
|
|
8784
8898
|
fileCount += 1;
|
|
8785
8899
|
}
|
|
8786
8900
|
return {
|
|
@@ -9022,6 +9136,25 @@ async function restoreBackup(archivePath, dbDir, options = {}) {
|
|
|
9022
9136
|
incoming.push(name);
|
|
9023
9137
|
}
|
|
9024
9138
|
if (incoming.length === 0) throw new Error(message("store.backupNoDatabases"));
|
|
9139
|
+
const { DatabaseSync } = await import("node:sqlite");
|
|
9140
|
+
for (const name of await readdir(dbDir)) {
|
|
9141
|
+
if (!name.endsWith(".db")) continue;
|
|
9142
|
+
const path = join(dbDir, name);
|
|
9143
|
+
if (!(await stat(path)).isFile()) continue;
|
|
9144
|
+
let probe;
|
|
9145
|
+
try {
|
|
9146
|
+
probe = new DatabaseSync(path);
|
|
9147
|
+
if (probe.prepare("PRAGMA wal_checkpoint(TRUNCATE)").all()[0]?.busy !== 0) throw new Error(message("store.restoreBusyWriters", { file: name }));
|
|
9148
|
+
} catch (error) {
|
|
9149
|
+
if (error instanceof Error && error.message.includes(name)) throw error;
|
|
9150
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
9151
|
+
throw new Error(`${name}: ${reason}`, { cause: error });
|
|
9152
|
+
} finally {
|
|
9153
|
+
try {
|
|
9154
|
+
probe?.close();
|
|
9155
|
+
} catch {}
|
|
9156
|
+
}
|
|
9157
|
+
}
|
|
9025
9158
|
const stamp = `${(/* @__PURE__ */ new Date()).toISOString().replaceAll(":", "-").slice(0, 19)}-${randomUUID()}`;
|
|
9026
9159
|
const current = [];
|
|
9027
9160
|
for (const name of await readdir(dbDir)) {
|
|
@@ -9103,30 +9236,16 @@ async function untarGz(archivePath, destDir) {
|
|
|
9103
9236
|
//#region src/telemetry/aggregate.ts
|
|
9104
9237
|
/** 聚合窗口(默认 7 天,可由路由 query 覆盖)。 */
|
|
9105
9238
|
const DEFAULT_WINDOW_DAYS = 7;
|
|
9106
|
-
/** 从单库 op_log 统计各 op
|
|
9239
|
+
/** 从单库 op_log 统计各 op 的计数(窗口内)——SQL GROUP BY,不物化明细行(审计 N1)。 */
|
|
9107
9240
|
async function countByOp(store, sinceMs) {
|
|
9108
|
-
|
|
9109
|
-
const counts = {};
|
|
9110
|
-
for (const op of ops) {
|
|
9111
|
-
if (op.at < sinceMs) continue;
|
|
9112
|
-
counts[op.op] = (counts[op.op] ?? 0) + 1;
|
|
9113
|
-
}
|
|
9114
|
-
return counts;
|
|
9241
|
+
return store.countOps(sinceMs);
|
|
9115
9242
|
}
|
|
9116
|
-
/** 按天汇总 op_log
|
|
9117
|
-
* 包含 write/update/ingest-done/ingest-request/distill-request/consolidation/restore/forget
|
|
9243
|
+
/** 按天汇总 op_log 中的写侧活动(热力日历)。返回的 dayMs 是该日 0 点的本地时间戳。
|
|
9244
|
+
* 包含 write/update/ingest-done/ingest-request/distill-request/consolidation/restore/forget,
|
|
9245
|
+
* 排除纯读侧(search/outcome-report)。折算两段走:SQL 按 UTC 天分组(吃 op_log_at 索引),
|
|
9246
|
+
* JS 用「UTC 正午」过 setHours 映射成本地日——正午法对夏令时切换 stable(对抗审查 P2-5)。 */
|
|
9118
9247
|
async function countByDay(store, sinceMs, untilMs) {
|
|
9119
|
-
const
|
|
9120
|
-
const counts = /* @__PURE__ */ new Map();
|
|
9121
|
-
const dayMs = (ms) => {
|
|
9122
|
-
const d = new Date(ms);
|
|
9123
|
-
d.setHours(0, 0, 0, 0);
|
|
9124
|
-
return d.getTime();
|
|
9125
|
-
};
|
|
9126
|
-
const startDay = dayMs(sinceMs);
|
|
9127
|
-
const endDay = dayMs(untilMs);
|
|
9128
|
-
for (let t = startDay; t <= endDay; t += 864e5) counts.set(t, 0);
|
|
9129
|
-
const writeOps = /* @__PURE__ */ new Set([
|
|
9248
|
+
const rows = await store.countOpsByDay(sinceMs, untilMs, [
|
|
9130
9249
|
"write",
|
|
9131
9250
|
"update",
|
|
9132
9251
|
"ingest-done",
|
|
@@ -9136,11 +9255,16 @@ async function countByDay(store, sinceMs, untilMs) {
|
|
|
9136
9255
|
"restore",
|
|
9137
9256
|
"forget"
|
|
9138
9257
|
]);
|
|
9139
|
-
|
|
9140
|
-
|
|
9141
|
-
|
|
9142
|
-
|
|
9143
|
-
|
|
9258
|
+
const counts = /* @__PURE__ */ new Map();
|
|
9259
|
+
const dayMs = (ms) => {
|
|
9260
|
+
const d = new Date(ms);
|
|
9261
|
+
d.setHours(0, 0, 0, 0);
|
|
9262
|
+
return d.getTime();
|
|
9263
|
+
};
|
|
9264
|
+
for (let t = dayMs(sinceMs); t <= dayMs(untilMs); t += 864e5) counts.set(t, 0);
|
|
9265
|
+
for (const row of rows) {
|
|
9266
|
+
const local = dayMs(row.utcDay * 864e5 + 432e5);
|
|
9267
|
+
counts.set(local, (counts.get(local) ?? 0) + row.count);
|
|
9144
9268
|
}
|
|
9145
9269
|
return counts;
|
|
9146
9270
|
}
|
|
@@ -9181,15 +9305,15 @@ async function aggregateTelemetry(openStore, windowDays = DEFAULT_WINDOW_DAYS, s
|
|
|
9181
9305
|
countsAgg.ingestRequests += counts["ingest-request"] ?? 0;
|
|
9182
9306
|
countsAgg.ingestDones += counts["ingest-done"] ?? 0;
|
|
9183
9307
|
countsAgg.searches += counts["search-rewrite-request"] ?? 0;
|
|
9184
|
-
countsAgg.writes += counts
|
|
9185
|
-
countsAgg.updates += counts
|
|
9186
|
-
countsAgg.forgets += counts
|
|
9187
|
-
countsAgg.restores += counts
|
|
9308
|
+
countsAgg.writes += counts.write ?? 0;
|
|
9309
|
+
countsAgg.updates += counts.update ?? 0;
|
|
9310
|
+
countsAgg.forgets += counts.forget ?? 0;
|
|
9311
|
+
countsAgg.restores += counts.restore ?? 0;
|
|
9188
9312
|
countsAgg.distillRequests += counts["distill-request"] ?? 0;
|
|
9189
9313
|
countsAgg.compressRequests += counts["compress-request"] ?? 0;
|
|
9190
9314
|
countsAgg.searchRewrites += counts["search-rewrite-request"] ?? 0;
|
|
9191
9315
|
countsAgg.consumptions += counts["outcome-report"] ?? 0;
|
|
9192
|
-
countsAgg.consolidations += counts
|
|
9316
|
+
countsAgg.consolidations += counts.consolidation ?? 0;
|
|
9193
9317
|
countsAgg.reviewAnswers += counts["review-answer"] ?? 0;
|
|
9194
9318
|
scopeViews.push({
|
|
9195
9319
|
scope,
|
|
@@ -9494,12 +9618,12 @@ function toBoolean(value) {
|
|
|
9494
9618
|
}
|
|
9495
9619
|
/** 从 query 或 body 收敛历史回填规则(未给或非法的字段留给配置默认值)。 */
|
|
9496
9620
|
function historyRulesOf(raw) {
|
|
9497
|
-
const days = toNumber(raw
|
|
9498
|
-
const maxTurnsPerSession = toNumber(raw
|
|
9499
|
-
const maxTotalTurns = toNumber(raw
|
|
9500
|
-
const includeSubagents = toBoolean(raw
|
|
9501
|
-
const includeSeeded = toBoolean(raw
|
|
9502
|
-
const includeNoCwd = toBoolean(raw
|
|
9621
|
+
const days = toNumber(raw.days);
|
|
9622
|
+
const maxTurnsPerSession = toNumber(raw.maxTurnsPerSession);
|
|
9623
|
+
const maxTotalTurns = toNumber(raw.maxTotalTurns);
|
|
9624
|
+
const includeSubagents = toBoolean(raw.includeSubagents);
|
|
9625
|
+
const includeSeeded = toBoolean(raw.includeSeeded);
|
|
9626
|
+
const includeNoCwd = toBoolean(raw.includeNoCwd);
|
|
9503
9627
|
return {
|
|
9504
9628
|
...days === void 0 ? {} : { days },
|
|
9505
9629
|
...maxTurnsPerSession === void 0 ? {} : { maxTurnsPerSession },
|
|
@@ -9538,7 +9662,7 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
9538
9662
|
};
|
|
9539
9663
|
/** POST body 里的项目选择器(面板所有写操作都会带)。 */
|
|
9540
9664
|
const adoptBodySelector = (body) => {
|
|
9541
|
-
const raw = body?.
|
|
9665
|
+
const raw = body?.project;
|
|
9542
9666
|
if (typeof raw === "string" && raw !== "") selectorRef.value = raw;
|
|
9543
9667
|
};
|
|
9544
9668
|
try {
|
|
@@ -9920,32 +10044,6 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
9920
10044
|
json(res, 200, view);
|
|
9921
10045
|
return;
|
|
9922
10046
|
}
|
|
9923
|
-
if (req.method === "GET" && route === "review-due") {
|
|
9924
|
-
const scope = scopeOf$1(url.searchParams.get("scope"), "user");
|
|
9925
|
-
const limit = Math.min(50, Math.max(1, Number(url.searchParams.get("limit") ?? 20) || 20));
|
|
9926
|
-
const now = Date.now();
|
|
9927
|
-
const due = await (await storeFor(scope)).dueReviews(now, limit);
|
|
9928
|
-
json(res, 200, {
|
|
9929
|
-
scope,
|
|
9930
|
-
count: due.length,
|
|
9931
|
-
now,
|
|
9932
|
-
items: due.map((record) => ({
|
|
9933
|
-
id: record.id,
|
|
9934
|
-
kind: record.kind,
|
|
9935
|
-
scope: record.scope,
|
|
9936
|
-
importance: record.importance,
|
|
9937
|
-
confidence: record.confidence,
|
|
9938
|
-
...record.slot === void 0 ? {} : { slot: record.slot },
|
|
9939
|
-
...typeof record.imagery?.caption === "string" ? { caption: record.imagery.caption } : {},
|
|
9940
|
-
...record.review?.nextReviewAt === null || record.review?.nextReviewAt === void 0 ? {} : { overdueDays: Math.max(0, Math.floor((now - record.review.nextReviewAt) / 864e5)) },
|
|
9941
|
-
...record.review === void 0 ? {} : {
|
|
9942
|
-
reps: record.review.reps,
|
|
9943
|
-
intervalDays: record.review.intervalDays
|
|
9944
|
-
}
|
|
9945
|
-
}))
|
|
9946
|
-
});
|
|
9947
|
-
return;
|
|
9948
|
-
}
|
|
9949
10047
|
if (req.method === "GET" && route === "export") {
|
|
9950
10048
|
const scope = scopeOf$1(url.searchParams.get("scope"), "user");
|
|
9951
10049
|
const format = url.searchParams.get("format") === "json" ? "json" : "markdown";
|
|
@@ -10092,16 +10190,16 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
10092
10190
|
if (rawStatus !== null && rawStatus !== "" && rawStatus !== "all") {
|
|
10093
10191
|
for (const s of rawStatus.split(",")) if (s === "active" || s === "archived" || s === "forgotten") includeStatuses.add(s);
|
|
10094
10192
|
}
|
|
10193
|
+
const limit = Math.min(1e3, Math.max(50, Number(url.searchParams.get("limit") ?? 400) || 400));
|
|
10095
10194
|
const data = await (await storeFor(scope)).exportAll();
|
|
10096
|
-
const nodes = data.records.filter((r) => includeStatuses.has(r.status)).map((r) => ({
|
|
10195
|
+
const nodes = data.records.filter((r) => includeStatuses.has(r.status)).sort((a, b) => b.importance * b.confidence - a.importance * a.confidence).slice(0, limit).map((r) => ({
|
|
10097
10196
|
id: r.id,
|
|
10098
10197
|
scope: r.scope,
|
|
10099
10198
|
kind: r.kind,
|
|
10100
10199
|
status: r.status,
|
|
10101
10200
|
importance: r.importance,
|
|
10102
10201
|
confidence: r.confidence,
|
|
10103
|
-
title: r.content.length > 40 ? `${r.content.slice(0, 40)}…` : r.content
|
|
10104
|
-
content: r.content
|
|
10202
|
+
title: r.content.length > 40 ? `${r.content.slice(0, 40)}…` : r.content
|
|
10105
10203
|
}));
|
|
10106
10204
|
const allowedIds = new Set(nodes.map((n) => n.id));
|
|
10107
10205
|
json(res, 200, {
|
|
@@ -10112,7 +10210,8 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
10112
10210
|
from: e.from,
|
|
10113
10211
|
to: e.to,
|
|
10114
10212
|
type: e.type
|
|
10115
|
-
}))
|
|
10213
|
+
})),
|
|
10214
|
+
total: data.records.filter((r) => includeStatuses.has(r.status)).length
|
|
10116
10215
|
});
|
|
10117
10216
|
return;
|
|
10118
10217
|
}
|
|
@@ -10233,7 +10332,7 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
10233
10332
|
if (!guardWrite(req, res)) return;
|
|
10234
10333
|
const body = await readBody(req, res, deps.language);
|
|
10235
10334
|
if (body === null) return;
|
|
10236
|
-
const set = body
|
|
10335
|
+
const set = body.set;
|
|
10237
10336
|
if (!isDictionarySet(set)) {
|
|
10238
10337
|
json(res, 400, {
|
|
10239
10338
|
ok: false,
|
|
@@ -10241,18 +10340,18 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
10241
10340
|
});
|
|
10242
10341
|
return;
|
|
10243
10342
|
}
|
|
10244
|
-
json(res, 200, deps.dictionary.write(set, entriesFromRequest(body
|
|
10343
|
+
json(res, 200, deps.dictionary.write(set, entriesFromRequest(body.entries), languageOf(body.language)));
|
|
10245
10344
|
return;
|
|
10246
10345
|
}
|
|
10247
10346
|
if (req.method === "POST" && route === "dictionary-create") {
|
|
10248
10347
|
if (!guardWrite(req, res)) return;
|
|
10249
10348
|
const body = await readBody(req, res, deps.language);
|
|
10250
10349
|
if (body === null) return;
|
|
10251
|
-
const language = languageOf(body
|
|
10350
|
+
const language = languageOf(body.language);
|
|
10252
10351
|
if (language === void 0) {
|
|
10253
10352
|
json(res, 400, {
|
|
10254
10353
|
ok: false,
|
|
10255
|
-
error: `unknown language: ${String(body
|
|
10354
|
+
error: `unknown language: ${String(body.language)}`
|
|
10256
10355
|
});
|
|
10257
10356
|
return;
|
|
10258
10357
|
}
|
|
@@ -10266,7 +10365,7 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
10266
10365
|
if (!guardWrite(req, res)) return;
|
|
10267
10366
|
const body = await readBody(req, res, deps.language);
|
|
10268
10367
|
if (body === null) return;
|
|
10269
|
-
const set = body
|
|
10368
|
+
const set = body.set;
|
|
10270
10369
|
if (!isDictionarySet(set)) {
|
|
10271
10370
|
json(res, 400, {
|
|
10272
10371
|
ok: false,
|
|
@@ -10274,7 +10373,7 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
10274
10373
|
});
|
|
10275
10374
|
return;
|
|
10276
10375
|
}
|
|
10277
|
-
const terms = (Array.isArray(body
|
|
10376
|
+
const terms = (Array.isArray(body.terms) ? body.terms : []).filter((term) => typeof term === "string");
|
|
10278
10377
|
json(res, 200, await deps.dictionaryFill(set, terms));
|
|
10279
10378
|
return;
|
|
10280
10379
|
}
|
|
@@ -10305,7 +10404,7 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
10305
10404
|
if (!guardWrite(req, res)) return;
|
|
10306
10405
|
const body = await readBody(req, res, deps.language);
|
|
10307
10406
|
if (body === null) return;
|
|
10308
|
-
const verdict = deps.palaceGuard.check(typeof body
|
|
10407
|
+
const verdict = deps.palaceGuard.check(typeof body.language === "string" ? body.language : "", Date.now(), deps.palaceChangeWindowSec);
|
|
10309
10408
|
if (!verdict.ok) {
|
|
10310
10409
|
json(res, 409, {
|
|
10311
10410
|
ok: false,
|
|
@@ -10314,7 +10413,7 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
10314
10413
|
return;
|
|
10315
10414
|
}
|
|
10316
10415
|
deps.palaceGuard.close();
|
|
10317
|
-
const written = await deps.writePalaceLanguage(String(body
|
|
10416
|
+
const written = await deps.writePalaceLanguage(String(body.language));
|
|
10318
10417
|
if (!written.ok) {
|
|
10319
10418
|
json(res, 200, {
|
|
10320
10419
|
...written,
|
|
@@ -10336,7 +10435,7 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
10336
10435
|
if (!guardWrite(req, res)) return;
|
|
10337
10436
|
const body = await readBody(req, res, deps.language);
|
|
10338
10437
|
if (body === null) return;
|
|
10339
|
-
const wanted = body
|
|
10438
|
+
const wanted = body.language;
|
|
10340
10439
|
if (typeof wanted !== "string" || !isLanguageCode(wanted)) {
|
|
10341
10440
|
json(res, 400, {
|
|
10342
10441
|
ok: false,
|
|
@@ -10533,8 +10632,8 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
10533
10632
|
return;
|
|
10534
10633
|
}
|
|
10535
10634
|
try {
|
|
10536
|
-
const result = await restoreBackup(normalized, deps.dbDir);
|
|
10537
10635
|
await deps.closeStores();
|
|
10636
|
+
const result = await restoreBackup(normalized, deps.dbDir);
|
|
10538
10637
|
json(res, 200, {
|
|
10539
10638
|
restored: result.restored,
|
|
10540
10639
|
meta: result.archiveMeta
|
|
@@ -10904,6 +11003,35 @@ async function readBaseCensus(input) {
|
|
|
10904
11003
|
problems
|
|
10905
11004
|
};
|
|
10906
11005
|
}
|
|
11006
|
+
/**
|
|
11007
|
+
* Язык, на котором стоит база, — по метке `meta.model_label`, лёгкое чтение.
|
|
11008
|
+
*
|
|
11009
|
+
* Отдельно от `readBaseCensus`: выбору модели при сборке плагина (issue #39) нужен
|
|
11010
|
+
* только язык существующей базы, а не полная опись векторов. Те же гарантии, что и
|
|
11011
|
+
* у описи: read-only соединение, отсутствие файла — «базы нет», а не ошибка,
|
|
11012
|
+
* любая проблема чтения — `undefined`, а не догадка.
|
|
11013
|
+
* @param path - путь к файлу базы.
|
|
11014
|
+
* @returns код языка из метки либо undefined (файла нет, метки нет, читать не удалось).
|
|
11015
|
+
*/
|
|
11016
|
+
async function readStoreLanguageLabel(path) {
|
|
11017
|
+
if (!existsSync(path)) return void 0;
|
|
11018
|
+
const { DatabaseSync } = await import("node:sqlite");
|
|
11019
|
+
let db;
|
|
11020
|
+
try {
|
|
11021
|
+
db = new DatabaseSync(path, { readOnly: true });
|
|
11022
|
+
db.exec("PRAGMA busy_timeout = 2000");
|
|
11023
|
+
const row = db.prepare("SELECT value FROM meta WHERE key = ?").get(MODEL_LABEL_KEY);
|
|
11024
|
+
if (row === void 0) return void 0;
|
|
11025
|
+
const language = languageOfLabels([row.value]);
|
|
11026
|
+
return typeof language === "string" ? language : void 0;
|
|
11027
|
+
} catch {
|
|
11028
|
+
return;
|
|
11029
|
+
} finally {
|
|
11030
|
+
try {
|
|
11031
|
+
db?.close();
|
|
11032
|
+
} catch {}
|
|
11033
|
+
}
|
|
11034
|
+
}
|
|
10907
11035
|
/** Имя таблицы или поля в SQL: в кавычках, кавычка внутри удваивается. */
|
|
10908
11036
|
function quoted(identifier) {
|
|
10909
11037
|
return `"${identifier.replace(/"/g, "\"\"")}"`;
|
|
@@ -11182,7 +11310,7 @@ var PalaceGuard = class {
|
|
|
11182
11310
|
const window = this.window;
|
|
11183
11311
|
if (window === null) return Math.max(0, Math.round(windowSec));
|
|
11184
11312
|
const left = Math.round((windowSec * 1e3 - (now - window.startedAt)) / 1e3);
|
|
11185
|
-
return left
|
|
11313
|
+
return Math.max(left, 0);
|
|
11186
11314
|
}
|
|
11187
11315
|
};
|
|
11188
11316
|
//#endregion
|
|
@@ -11499,11 +11627,11 @@ function migrateProjectDb(dbDir, identity, policy = "eager") {
|
|
|
11499
11627
|
if (policy === "conservative") return "deferred";
|
|
11500
11628
|
renameSync(legacy, next);
|
|
11501
11629
|
try {
|
|
11502
|
-
writeFileSync(`${legacy}.migrated-to`, JSON.stringify({
|
|
11630
|
+
writeFileSync(`${legacy}.migrated-to`, `${JSON.stringify({
|
|
11503
11631
|
migratedTo: identity.dbName,
|
|
11504
11632
|
claimedByCwd: identity.cwd,
|
|
11505
11633
|
claimedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
11506
|
-
})
|
|
11634
|
+
})}\n`, {
|
|
11507
11635
|
flag: "wx",
|
|
11508
11636
|
mode: 384
|
|
11509
11637
|
});
|
|
@@ -11953,6 +12081,14 @@ function rowToRecord(row) {
|
|
|
11953
12081
|
function blobToVec(blob) {
|
|
11954
12082
|
return new Float32Array(blob.buffer.slice(blob.byteOffset, blob.byteOffset + blob.byteLength));
|
|
11955
12083
|
}
|
|
12084
|
+
/**
|
|
12085
|
+
* 单位区间收口:NaN/∞/越界的 importance·confidence 一律拉回 [0,1](非有限数取备用值)。
|
|
12086
|
+
* 工具层不做这道检查(engram_save 直收模型给的数,`typeof NaN === 'number'` 也通过),
|
|
12087
|
+
* 库层是最后一道闸——`importance: 42` 会永久霸榜 topActive,进 session-start 注入。
|
|
12088
|
+
*/
|
|
12089
|
+
function clampUnit(value, fallback) {
|
|
12090
|
+
return Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : fallback;
|
|
12091
|
+
}
|
|
11956
12092
|
function entityRowToRecord(row) {
|
|
11957
12093
|
let aliases = [];
|
|
11958
12094
|
try {
|
|
@@ -12055,9 +12191,6 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12055
12191
|
});
|
|
12056
12192
|
const { DatabaseSync } = await import("node:sqlite");
|
|
12057
12193
|
const db = new DatabaseSync(path);
|
|
12058
|
-
db.exec("PRAGMA journal_mode = WAL;");
|
|
12059
|
-
db.exec("PRAGMA busy_timeout = 5000;");
|
|
12060
|
-
db.exec("PRAGMA synchronous = NORMAL;");
|
|
12061
12194
|
/** 手工事务:BEGIN/COMMIT/ROLLBACK(兼容 ^22.19 引擎范围)。 */
|
|
12062
12195
|
const withTransaction = (fn) => {
|
|
12063
12196
|
db.exec("BEGIN");
|
|
@@ -12069,7 +12202,36 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12069
12202
|
throw error;
|
|
12070
12203
|
}
|
|
12071
12204
|
};
|
|
12072
|
-
|
|
12205
|
+
/**
|
|
12206
|
+
* 库中已有向量的维度(按浮点数个数计,不按字节)。
|
|
12207
|
+
*
|
|
12208
|
+
* 打开时读一次,且两张带向量的表一起读:记忆与信念彼此比较,任何一边对不上
|
|
12209
|
+
* 都会让语义检索失真。库里还没有向量时维度未知——由第一次写入定下来,在那之前
|
|
12210
|
+
* 没有可拒绝的东西。
|
|
12211
|
+
*
|
|
12212
|
+
* 维度取占多数的那一批,而不是任意一行:库里混进过别的模型的向量时(换过模型、
|
|
12213
|
+
* 从备份恢复过、旧模型的归档残留),任意一行会把整库的维度判给少数派,此后
|
|
12214
|
+
* 正常模型的写入全部被拒,而原因(少数几行异维数据)在错误里看不见。多数派
|
|
12215
|
+
* 保证「这库现在算谁的库」由库里的多数回答;少数派只是被晾在一边,不参与比较。
|
|
12216
|
+
* 表格平局时取较小的维度:写入被拒是响的,静默沿用大维度才是危险的。
|
|
12217
|
+
*
|
|
12218
|
+
* 长度不是 4 的倍数的 BLOB 跳过:那是损坏的行,按它定维会得到 153.25 这样的
|
|
12219
|
+
* 小数,此后每一次写入都被拒绝,而真正的原因(一行坏数据)却看不见。
|
|
12220
|
+
*/
|
|
12221
|
+
const computeStoredDim = () => {
|
|
12222
|
+
const dims = db.prepare(`SELECT dim, COUNT(*) AS rows FROM (
|
|
12223
|
+
SELECT LENGTH(embedding) / ${Float32Array.BYTES_PER_ELEMENT} AS dim FROM nodes WHERE embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0
|
|
12224
|
+
UNION ALL
|
|
12225
|
+
SELECT LENGTH(embedding) / ${Float32Array.BYTES_PER_ELEMENT} AS dim FROM observations WHERE embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0
|
|
12226
|
+
) GROUP BY dim ORDER BY rows DESC, dim ASC`).all();
|
|
12227
|
+
return dims.length === 0 ? void 0 : dims[0].dim;
|
|
12228
|
+
};
|
|
12229
|
+
let storedDim;
|
|
12230
|
+
try {
|
|
12231
|
+
db.exec("PRAGMA journal_mode = WAL;");
|
|
12232
|
+
db.exec("PRAGMA busy_timeout = 5000;");
|
|
12233
|
+
db.exec("PRAGMA synchronous = NORMAL;");
|
|
12234
|
+
db.exec(`
|
|
12073
12235
|
CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
12074
12236
|
CREATE TABLE IF NOT EXISTS nodes (
|
|
12075
12237
|
id TEXT PRIMARY KEY, scope TEXT NOT NULL, kind TEXT NOT NULL, content TEXT NOT NULL,
|
|
@@ -12123,78 +12285,57 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12123
12285
|
CREATE INDEX IF NOT EXISTS facts_entity ON facts (entity_id);
|
|
12124
12286
|
CREATE INDEX IF NOT EXISTS observations_status ON observations (status, proof_count, updated_at);
|
|
12125
12287
|
`);
|
|
12126
|
-
|
|
12127
|
-
|
|
12128
|
-
|
|
12129
|
-
|
|
12130
|
-
|
|
12131
|
-
|
|
12132
|
-
|
|
12133
|
-
|
|
12134
|
-
db.close();
|
|
12135
|
-
throw new EngramError("SCHEMA_INCOMPATIBLE", message("store.schemaVersionTooNew", {
|
|
12288
|
+
const versionRow = db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get();
|
|
12289
|
+
if (versionRow === void 0) {
|
|
12290
|
+
db.exec(`CREATE INDEX IF NOT EXISTS nodes_slot ON nodes (slot_room, slot_index);
|
|
12291
|
+
CREATE INDEX IF NOT EXISTS nodes_review_due ON nodes (next_review_at);`);
|
|
12292
|
+
db.prepare("INSERT INTO meta (key, value) VALUES ('schema_version', ?)").run(String(SCHEMA_VERSION));
|
|
12293
|
+
} else {
|
|
12294
|
+
let version = Number(versionRow.value);
|
|
12295
|
+
if (!Number.isInteger(version) || version > SCHEMA_VERSION) throw new EngramError("SCHEMA_INCOMPATIBLE", message("store.schemaVersionTooNew", {
|
|
12136
12296
|
found: versionRow.value,
|
|
12137
12297
|
supported: SCHEMA_VERSION,
|
|
12138
12298
|
path
|
|
12139
12299
|
}));
|
|
12140
|
-
|
|
12141
|
-
|
|
12142
|
-
|
|
12143
|
-
|
|
12144
|
-
|
|
12145
|
-
|
|
12146
|
-
|
|
12147
|
-
}
|
|
12148
|
-
|
|
12149
|
-
|
|
12150
|
-
|
|
12151
|
-
if (migration === void 0) {
|
|
12152
|
-
db.close();
|
|
12153
|
-
throw new EngramError("SCHEMA_INCOMPATIBLE", message("store.schemaMigrationMissing", {
|
|
12300
|
+
if (version < SCHEMA_VERSION) try {
|
|
12301
|
+
await copyFile(path, `${path}.pre-migration`);
|
|
12302
|
+
} catch (error) {
|
|
12303
|
+
console.warn(message("store.preMigrationSnapshotFailed", {
|
|
12304
|
+
path,
|
|
12305
|
+
msg: error instanceof Error ? error.message : String(error)
|
|
12306
|
+
}));
|
|
12307
|
+
}
|
|
12308
|
+
while (version < SCHEMA_VERSION) {
|
|
12309
|
+
const migration = MIGRATIONS[String(version)];
|
|
12310
|
+
if (migration === void 0) throw new EngramError("SCHEMA_INCOMPATIBLE", message("store.schemaMigrationMissing", {
|
|
12154
12311
|
found: version,
|
|
12155
12312
|
supported: SCHEMA_VERSION,
|
|
12156
12313
|
path
|
|
12157
12314
|
}));
|
|
12158
|
-
|
|
12159
|
-
|
|
12160
|
-
|
|
12161
|
-
|
|
12162
|
-
|
|
12163
|
-
|
|
12164
|
-
|
|
12165
|
-
|
|
12315
|
+
withTransaction(() => {
|
|
12316
|
+
const safe = migration.split(";").map((stmt) => stmt.trim()).filter((stmt) => stmt !== "").filter((stmt) => {
|
|
12317
|
+
const match = /^ALTER\s+TABLE\s+(\w+)\s+ADD\s+COLUMN\s+(\w+)/i.exec(stmt);
|
|
12318
|
+
if (match === null) return true;
|
|
12319
|
+
const table = match[1];
|
|
12320
|
+
const column = match[2];
|
|
12321
|
+
return !db.prepare(`PRAGMA table_info(${table})`).all().some((c) => c.name === column);
|
|
12322
|
+
});
|
|
12323
|
+
for (const stmt of safe) db.exec(stmt);
|
|
12324
|
+
version += 1;
|
|
12325
|
+
db.prepare("UPDATE meta SET value = ? WHERE key = 'schema_version'").run(String(version));
|
|
12166
12326
|
});
|
|
12167
|
-
|
|
12168
|
-
version += 1;
|
|
12169
|
-
db.prepare("UPDATE meta SET value = ? WHERE key = 'schema_version'").run(String(version));
|
|
12170
|
-
});
|
|
12327
|
+
}
|
|
12171
12328
|
}
|
|
12329
|
+
db.exec(`CREATE INDEX IF NOT EXISTS op_log_op_detail ON op_log (op, detail);
|
|
12330
|
+
CREATE INDEX IF NOT EXISTS op_log_target ON op_log (target_id);
|
|
12331
|
+
CREATE INDEX IF NOT EXISTS op_log_at ON op_log (at);`);
|
|
12332
|
+
storedDim = computeStoredDim();
|
|
12333
|
+
} catch (error) {
|
|
12334
|
+
try {
|
|
12335
|
+
db.close();
|
|
12336
|
+
} catch {}
|
|
12337
|
+
throw error;
|
|
12172
12338
|
}
|
|
12173
|
-
/**
|
|
12174
|
-
* 库中已有向量的维度(按浮点数个数计,不按字节)。
|
|
12175
|
-
*
|
|
12176
|
-
* 打开时读一次,且两张带向量的表一起读:记忆与信念彼此比较,任何一边对不上
|
|
12177
|
-
* 都会让语义检索失真。库里还没有向量时维度未知——由第一次写入定下来,在那之前
|
|
12178
|
-
* 没有可拒绝的东西。
|
|
12179
|
-
*
|
|
12180
|
-
* 维度取占多数的那一批,而不是任意一行:库里混进过别的模型的向量时(换过模型、
|
|
12181
|
-
* 从备份恢复过、旧模型的归档残留),任意一行会把整库的维度判给少数派,此后
|
|
12182
|
-
* 正常模型的写入全部被拒,而原因(少数几行异维数据)在错误里看不见。多数派
|
|
12183
|
-
* 保证「这库现在算谁的库」由库里的多数回答;少数派只是被晾在一边,不参与比较。
|
|
12184
|
-
* 表格平局时取较小的维度:写入被拒是响的,静默沿用大维度才是危险的。
|
|
12185
|
-
*
|
|
12186
|
-
* 长度不是 4 的倍数的 BLOB 跳过:那是损坏的行,按它定维会得到 153.25 这样的
|
|
12187
|
-
* 小数,此后每一次写入都被拒绝,而真正的原因(一行坏数据)却看不见。
|
|
12188
|
-
*/
|
|
12189
|
-
const computeStoredDim = () => {
|
|
12190
|
-
const dims = db.prepare(`SELECT dim, COUNT(*) AS rows FROM (
|
|
12191
|
-
SELECT LENGTH(embedding) / ${Float32Array.BYTES_PER_ELEMENT} AS dim FROM nodes WHERE embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0
|
|
12192
|
-
UNION ALL
|
|
12193
|
-
SELECT LENGTH(embedding) / ${Float32Array.BYTES_PER_ELEMENT} AS dim FROM observations WHERE embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0
|
|
12194
|
-
) GROUP BY dim ORDER BY rows DESC, dim ASC`).all();
|
|
12195
|
-
return dims.length === 0 ? void 0 : dims[0].dim;
|
|
12196
|
-
};
|
|
12197
|
-
let storedDim = computeStoredDim();
|
|
12198
12339
|
/** 维度不符导致读取降级时只提醒一次:检索是高频路径,逐次告警会淹掉日志。 */
|
|
12199
12340
|
let warnedDegradedRead = false;
|
|
12200
12341
|
/**
|
|
@@ -12211,6 +12352,9 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12211
12352
|
expected,
|
|
12212
12353
|
found: length
|
|
12213
12354
|
}));
|
|
12355
|
+
if (vector instanceof Float32Array) {
|
|
12356
|
+
for (let i = 0; i < vector.length; i += 1) if (!Number.isFinite(vector[i])) throw new EngramError("EMBEDDER_INVALID_VECTOR", message("store.embedderInvalidVector"));
|
|
12357
|
+
}
|
|
12214
12358
|
storedDim = length;
|
|
12215
12359
|
};
|
|
12216
12360
|
const sqlGet = db.prepare("SELECT * FROM nodes WHERE id = ?");
|
|
@@ -12248,6 +12392,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12248
12392
|
SELECT id FROM walk WHERE depth > 0
|
|
12249
12393
|
)`);
|
|
12250
12394
|
const sqlRecentOps = db.prepare("SELECT at, op, target_id, detail FROM op_log ORDER BY seq DESC LIMIT ?");
|
|
12395
|
+
const sqlCountOps = db.prepare("SELECT op, COUNT(*) AS n FROM op_log WHERE at >= ? GROUP BY op");
|
|
12251
12396
|
const sqlRevisionInsert = db.prepare("INSERT INTO nodes_revisions (node_id, content, kind, importance, superseded_at) VALUES (?, ?, ?, ?, ?)");
|
|
12252
12397
|
const sqlRevisionsById = db.prepare("SELECT content, kind, importance, superseded_at FROM nodes_revisions WHERE node_id = ? ORDER BY superseded_at DESC");
|
|
12253
12398
|
const sqlTopActive = db.prepare("SELECT * FROM nodes WHERE scope = ? AND status = 'active' ORDER BY importance DESC, confidence DESC LIMIT ?");
|
|
@@ -12351,7 +12496,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12351
12496
|
const sqlObservationSetStatus = db.prepare("UPDATE observations SET status = ?, updated_at = ?, last_validated_at = ? WHERE id = ?");
|
|
12352
12497
|
const sqlObservationMarkStale = db.prepare("UPDATE observations SET status = 'stale' WHERE id = ?");
|
|
12353
12498
|
const sqlObservationRefute = db.prepare("UPDATE observations SET status = 'refuted', updated_at = ? WHERE id = ?");
|
|
12354
|
-
const sqlObservationActiveVectors = db.prepare(
|
|
12499
|
+
const sqlObservationActiveVectors = db.prepare(`SELECT * FROM observations WHERE status != 'refuted' AND embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0`);
|
|
12355
12500
|
const sqlPurgeObservations = db.prepare("DELETE FROM observations");
|
|
12356
12501
|
/** FTS 道:按 scope 集合检索(占位符动态生成,scope 集合由调用方去重);rooms 非空时只查指定房间。 */
|
|
12357
12502
|
const ftsSearch = (match, scopes, rooms) => {
|
|
@@ -12361,11 +12506,128 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12361
12506
|
WHERE nodes_fts MATCH ? AND n.status = 'active' AND n.scope IN (${placeholders})${roomCond}
|
|
12362
12507
|
ORDER BY bm25(nodes_fts) LIMIT ${RANK_POOL}`).all(match, ...scopes, ...rooms ?? []);
|
|
12363
12508
|
};
|
|
12364
|
-
/** 向量候选池:active 且带向量的条目,按 scope 集合过滤(占位符动态生成);rooms 非空时只查指定房间。
|
|
12509
|
+
/** 向量候选池:active 且带向量的条目,按 scope 集合过滤(占位符动态生成);rooms 非空时只查指定房间。
|
|
12510
|
+
* 长度非 4 倍数的 BLOB(损坏行)不进池:读道按 computeStoredDim 同一标准跳过,
|
|
12511
|
+
* 否则一个坏行让 blobToVec 抛 RangeError,整个 engram_search 报错而不是降级(审计 N10)。 */
|
|
12365
12512
|
const vectorPool = (scopes, rooms) => {
|
|
12366
12513
|
const placeholders = scopes.map(() => "?").join(",");
|
|
12367
12514
|
const roomCond = rooms === void 0 || rooms.length === 0 ? "" : ` AND slot_room IN (${rooms.map(() => "?").join(",")})`;
|
|
12368
|
-
return db.prepare(`SELECT * FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND scope IN (${placeholders})${roomCond}`).all(...scopes, ...rooms ?? []);
|
|
12515
|
+
return db.prepare(`SELECT * FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0 AND scope IN (${placeholders})${roomCond}`).all(...scopes, ...rooms ?? []);
|
|
12516
|
+
};
|
|
12517
|
+
/**
|
|
12518
|
+
* 向量道的进程内打包矩阵(审计 P1-2)。双道检索每次查询、写入处置(最近邻/矛盾探测)
|
|
12519
|
+
* 每次写入都要全池余弦:旧实现逐行 new Float32Array + 逐行调 cosine——一万条即单扫
|
|
12520
|
+
* 15MB 垃圾、380 万次乘加,scope=all 一次检索最多十二连扫。打包成单个 Float32Array
|
|
12521
|
+
* 后用紧凑点积循环一次算完:一次分配、零逐行对象、无函数调用开销。
|
|
12522
|
+
*
|
|
12523
|
+
* 失效:gen 取 op_log 的 MAX(seq)(PK 上 O(1))。所有会改变候选集的写路径都写 op_log
|
|
12524
|
+
* (insertRecord/update/forget/forgetWithTombstone/restore/import/decay/reinforce),
|
|
12525
|
+
* 例外只有 attachEmbeddings / rewriteVectors / purge 三个——它们各自显式
|
|
12526
|
+
* bumpVectorMatrix()。新增会改 nodes.embedding 或 status 的写路径时必须补失效,
|
|
12527
|
+
* 否则检索对着旧矩阵打分——这条约定与「加列走 MIGRATIONS」同级,忘不得。
|
|
12528
|
+
*
|
|
12529
|
+
* 上限:超过 VECTOR_MATRIX_MAX_ROWS 不缓存(常数内存上界),调用方回落逐行流式扫描
|
|
12530
|
+
* ——那个量级该上 sqlite-vec,而不是把 150MB 钉在进程里。
|
|
12531
|
+
*/
|
|
12532
|
+
const VECTOR_MATRIX_MAX_ROWS = 5e4;
|
|
12533
|
+
const sqlMatrixGen = db.prepare("SELECT COALESCE(MAX(seq), 0) AS g FROM op_log");
|
|
12534
|
+
const sqlMatrixCount = db.prepare(`SELECT COUNT(*) AS n FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0`);
|
|
12535
|
+
const sqlMatrixPool = db.prepare(`SELECT id, scope, slot_room, embedding FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0`);
|
|
12536
|
+
const SCOPE_ORDER = [
|
|
12537
|
+
"user",
|
|
12538
|
+
"project",
|
|
12539
|
+
"shared"
|
|
12540
|
+
];
|
|
12541
|
+
let vecMatrix;
|
|
12542
|
+
/** overflow 判定按 gen 缓存:超限库里每次查询都 COUNT 一次(O(索引)),不再物化全池。 */
|
|
12543
|
+
let vecOverflowGen = -1;
|
|
12544
|
+
const roomTable = /* @__PURE__ */ new Map();
|
|
12545
|
+
const bumpVectorMatrix = () => {
|
|
12546
|
+
vecMatrix = void 0;
|
|
12547
|
+
vecOverflowGen = -1;
|
|
12548
|
+
};
|
|
12549
|
+
const vectorMatrix = () => {
|
|
12550
|
+
const gen = Number(sqlMatrixGen.get().g);
|
|
12551
|
+
if (vecMatrix !== void 0 && vecMatrix.gen === gen) return vecMatrix;
|
|
12552
|
+
if (vecOverflowGen === gen) return "overflow";
|
|
12553
|
+
const count = Number(sqlMatrixCount.get().n);
|
|
12554
|
+
if (count === 0) {
|
|
12555
|
+
vecMatrix = void 0;
|
|
12556
|
+
return;
|
|
12557
|
+
}
|
|
12558
|
+
if (count > VECTOR_MATRIX_MAX_ROWS) {
|
|
12559
|
+
vecOverflowGen = gen;
|
|
12560
|
+
return "overflow";
|
|
12561
|
+
}
|
|
12562
|
+
const all = sqlMatrixPool.all();
|
|
12563
|
+
const dimCounts = /* @__PURE__ */ new Map();
|
|
12564
|
+
for (const row of all) {
|
|
12565
|
+
const d = row.embedding.byteLength / Float32Array.BYTES_PER_ELEMENT;
|
|
12566
|
+
dimCounts.set(d, (dimCounts.get(d) ?? 0) + 1);
|
|
12567
|
+
}
|
|
12568
|
+
const dim = [...dimCounts.entries()].sort((a, b) => b[1] - a[1] || a[0] - b[0])[0][0];
|
|
12569
|
+
const rows = dimCounts.size === 1 ? all : all.filter((row) => row.embedding.byteLength / Float32Array.BYTES_PER_ELEMENT === dim);
|
|
12570
|
+
const packed = new Float32Array(rows.length * dim);
|
|
12571
|
+
const scopeIdx = new Uint8Array(rows.length);
|
|
12572
|
+
const roomIdx = new Uint16Array(rows.length);
|
|
12573
|
+
for (let i = 0; i < rows.length; i += 1) {
|
|
12574
|
+
const row = rows[i];
|
|
12575
|
+
const blob = row.embedding;
|
|
12576
|
+
const floats = new Float32Array(blob.buffer, blob.byteOffset, blob.byteLength / Float32Array.BYTES_PER_ELEMENT);
|
|
12577
|
+
let norm = 0;
|
|
12578
|
+
for (let k = 0; k < floats.length; k += 1) norm += floats[k] * floats[k];
|
|
12579
|
+
norm = Math.sqrt(norm);
|
|
12580
|
+
const base = i * dim;
|
|
12581
|
+
if (norm > 0) for (let k = 0; k < floats.length; k += 1) packed[base + k] = floats[k] / norm;
|
|
12582
|
+
scopeIdx[i] = Math.max(0, SCOPE_ORDER.indexOf(row.scope));
|
|
12583
|
+
if (row.slot_room === null) roomIdx[i] = 65535;
|
|
12584
|
+
else {
|
|
12585
|
+
let idx = roomTable.get(row.slot_room);
|
|
12586
|
+
if (idx === void 0) {
|
|
12587
|
+
idx = roomTable.size;
|
|
12588
|
+
roomTable.set(row.slot_room, idx);
|
|
12589
|
+
}
|
|
12590
|
+
roomIdx[i] = idx;
|
|
12591
|
+
}
|
|
12592
|
+
}
|
|
12593
|
+
vecMatrix = {
|
|
12594
|
+
gen,
|
|
12595
|
+
dim,
|
|
12596
|
+
ids: rows.map((row) => row.id),
|
|
12597
|
+
packed,
|
|
12598
|
+
scopeIdx,
|
|
12599
|
+
roomIdx
|
|
12600
|
+
};
|
|
12601
|
+
return vecMatrix;
|
|
12602
|
+
};
|
|
12603
|
+
/** 归一化副本(零向量原样返回)。查询侧每次调用做一次,行侧在打包时已归一。 */
|
|
12604
|
+
const unit = (vector) => {
|
|
12605
|
+
let norm = 0;
|
|
12606
|
+
for (let k = 0; k < vector.length; k += 1) norm += vector[k] * vector[k];
|
|
12607
|
+
norm = Math.sqrt(norm);
|
|
12608
|
+
if (norm === 0) return vector;
|
|
12609
|
+
const out = new Float32Array(vector.length);
|
|
12610
|
+
for (let k = 0; k < vector.length; k += 1) out[k] = vector[k] / norm;
|
|
12611
|
+
return out;
|
|
12612
|
+
};
|
|
12613
|
+
/** 紧凑点积:矩阵第 i 行与单位化 query 的内积(行已在打包时归一化,等价余弦)。 */
|
|
12614
|
+
const dotAt = (matrix, i, queryUnit) => {
|
|
12615
|
+
const base = i * matrix.dim;
|
|
12616
|
+
let dot = 0;
|
|
12617
|
+
for (let k = 0; k < matrix.dim; k += 1) dot += queryUnit[k] * matrix.packed[base + k];
|
|
12618
|
+
return dot;
|
|
12619
|
+
};
|
|
12620
|
+
/** RRF 计分累加:同 id 多道命中合并(via 升级为 both)。 */
|
|
12621
|
+
const addScore = (scores, id, add, via) => {
|
|
12622
|
+
const existing = scores.get(id);
|
|
12623
|
+
if (existing === void 0) scores.set(id, {
|
|
12624
|
+
score: add,
|
|
12625
|
+
via
|
|
12626
|
+
});
|
|
12627
|
+
else scores.set(id, {
|
|
12628
|
+
score: existing.score + add,
|
|
12629
|
+
via: "both"
|
|
12630
|
+
});
|
|
12369
12631
|
};
|
|
12370
12632
|
const getRow = (id) => sqlGet.get(id);
|
|
12371
12633
|
/**
|
|
@@ -12376,7 +12638,9 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12376
12638
|
if (embedding !== null) guardDimension(embedding);
|
|
12377
12639
|
const stored = embedding === null ? null : embedding instanceof Float32Array ? vecToBlob(embedding) : embedding;
|
|
12378
12640
|
const initialReview = input.initialReviewAt ?? null;
|
|
12379
|
-
|
|
12641
|
+
const safeImportance = clampUnit(importance, .5);
|
|
12642
|
+
const safeConfidence = clampUnit(confidence, .5);
|
|
12643
|
+
sqlInsert.run(id, input.scope, input.kind, content, safeImportance, safeConfidence, at, at, sourceSessionId, input.sourceRound ?? null, input.sourceSeq ?? null, stored, imageryToJson(imagery), input.slot?.room ?? null, input.slot?.index ?? null, input.imageryScore ?? null, initialReview, initialReview === null ? null : 2.5, initialReview === null ? null : 0);
|
|
12380
12644
|
sqlFtsInsert.run(id, tokenizeForFts(content));
|
|
12381
12645
|
sqlLog.run(at, op, id, JSON.stringify({
|
|
12382
12646
|
kind: input.kind,
|
|
@@ -12528,22 +12792,40 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12528
12792
|
});
|
|
12529
12793
|
let degraded = true;
|
|
12530
12794
|
if (queryVector !== void 0) try {
|
|
12531
|
-
|
|
12532
|
-
|
|
12533
|
-
|
|
12534
|
-
|
|
12535
|
-
|
|
12536
|
-
|
|
12537
|
-
|
|
12538
|
-
|
|
12539
|
-
via: "vec"
|
|
12795
|
+
const matrix = vectorMatrix();
|
|
12796
|
+
if (matrix === void 0) degraded = false;
|
|
12797
|
+
else if (matrix === "overflow") {
|
|
12798
|
+
vectorPool(query.scopes, query.rooms).map((row) => ({
|
|
12799
|
+
id: row.id,
|
|
12800
|
+
sim: cosine(queryVector, blobToVec(row.embedding))
|
|
12801
|
+
})).filter((entry) => entry.sim >= MIN_COSINE).sort((a, b) => b.sim - a.sim).slice(0, RANK_POOL).forEach((entry, index) => {
|
|
12802
|
+
addScore(scores, entry.id, 1 / (RRF_K + index + 1), "vec");
|
|
12540
12803
|
});
|
|
12541
|
-
|
|
12542
|
-
|
|
12543
|
-
|
|
12804
|
+
degraded = false;
|
|
12805
|
+
} else {
|
|
12806
|
+
if (queryVector.length !== matrix.dim) throw new EngramError("EMBEDDER_DIMENSION_MISMATCH", message("store.embedderDimensionMismatch", {
|
|
12807
|
+
expected: matrix.dim,
|
|
12808
|
+
found: queryVector.length
|
|
12809
|
+
}));
|
|
12810
|
+
const scopeMask = query.scopes.reduce((mask, scope) => mask | 1 << Math.max(0, SCOPE_ORDER.indexOf(scope)), 0);
|
|
12811
|
+
const roomSet = query.rooms === void 0 || query.rooms.length === 0 ? null : new Set(query.rooms.map((room) => roomTable.get(room) ?? -1));
|
|
12812
|
+
const queryUnit = unit(queryVector);
|
|
12813
|
+
const hits = [];
|
|
12814
|
+
for (let i = 0; i < matrix.ids.length; i += 1) {
|
|
12815
|
+
if ((scopeMask & 1 << matrix.scopeIdx[i]) === 0) continue;
|
|
12816
|
+
if (roomSet !== null && !roomSet.has(matrix.roomIdx[i])) continue;
|
|
12817
|
+
const sim = dotAt(matrix, i, queryUnit);
|
|
12818
|
+
if (sim >= MIN_COSINE) hits.push({
|
|
12819
|
+
index: i,
|
|
12820
|
+
sim
|
|
12821
|
+
});
|
|
12822
|
+
}
|
|
12823
|
+
hits.sort((a, b) => b.sim - a.sim);
|
|
12824
|
+
hits.slice(0, RANK_POOL).forEach((hit, index) => {
|
|
12825
|
+
addScore(scores, matrix.ids[hit.index], 1 / (RRF_K + index + 1), "vec");
|
|
12544
12826
|
});
|
|
12545
|
-
|
|
12546
|
-
|
|
12827
|
+
degraded = false;
|
|
12828
|
+
}
|
|
12547
12829
|
} catch (error) {
|
|
12548
12830
|
if (!isDimensionMismatch(error)) throw error;
|
|
12549
12831
|
if (!warnedDegradedRead) {
|
|
@@ -12977,6 +13259,25 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12977
13259
|
detail: op.detail
|
|
12978
13260
|
}));
|
|
12979
13261
|
},
|
|
13262
|
+
async countOps(sinceMs) {
|
|
13263
|
+
const rows = sqlCountOps.all(sinceMs);
|
|
13264
|
+
const counts = {};
|
|
13265
|
+
for (const row of rows) counts[row.op] = row.n;
|
|
13266
|
+
return counts;
|
|
13267
|
+
},
|
|
13268
|
+
async countOpsByDay(sinceMs, untilMs, ops) {
|
|
13269
|
+
const placeholders = ops.map(() => "?").join(", ");
|
|
13270
|
+
return db.prepare(`SELECT at / 86400000 AS utcDay, COUNT(*) AS n FROM op_log
|
|
13271
|
+
WHERE at >= ? AND at <= ? AND op IN (${placeholders}) GROUP BY utcDay`).all(sinceMs, untilMs, ...ops).map((row) => ({
|
|
13272
|
+
utcDay: Number(row.utcDay),
|
|
13273
|
+
count: row.n
|
|
13274
|
+
}));
|
|
13275
|
+
},
|
|
13276
|
+
async pruneOps(olderThanMs, keepOps) {
|
|
13277
|
+
const placeholders = keepOps.map(() => "?").join(", ");
|
|
13278
|
+
const result = db.prepare(`DELETE FROM op_log WHERE at < ? AND op NOT IN (${placeholders})`).run(olderThanMs, ...keepOps);
|
|
13279
|
+
return Number(result.changes);
|
|
13280
|
+
},
|
|
12980
13281
|
async stats() {
|
|
12981
13282
|
const statusRows = sqlCountBy.all();
|
|
12982
13283
|
const kindRows = sqlCountKind.all();
|
|
@@ -12990,9 +13291,9 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12990
13291
|
for (const row of statusRows) byStatus[row.status] = row.n;
|
|
12991
13292
|
const byKind = {};
|
|
12992
13293
|
for (const row of kindRows) byKind[row.kind] = row.n;
|
|
12993
|
-
const active = byStatus
|
|
12994
|
-
const archived = byStatus
|
|
12995
|
-
const forgotten = byStatus
|
|
13294
|
+
const active = byStatus.active ?? 0;
|
|
13295
|
+
const archived = byStatus.archived ?? 0;
|
|
13296
|
+
const forgotten = byStatus.forgotten ?? 0;
|
|
12996
13297
|
const total = active + archived + forgotten;
|
|
12997
13298
|
return {
|
|
12998
13299
|
total,
|
|
@@ -13074,7 +13375,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13074
13375
|
for (const c of eligible) {
|
|
13075
13376
|
const r = c.record;
|
|
13076
13377
|
const review = r.review;
|
|
13077
|
-
sqlImportInsert.run(r.id, r.scope, r.kind, c.content, r.importance, r.confidence, r.status, r.createdAt, r.lastAccessedAt, r.accessCount, r.sourceSessionId ?? null, r.sourceRound ?? null, r.sourceSeq ?? null, null, r.outcome ?? null, imageryToJson(r.imagery), r.slot?.room ?? null, r.slot?.index ?? null, r.imageryScore ?? null, review?.nextReviewAt ?? null, review?.easeFactor ?? null, review?.intervalDays ?? null, review?.reps ?? null);
|
|
13378
|
+
sqlImportInsert.run(r.id, r.scope, r.kind, c.content, clampUnit(r.importance, .5), clampUnit(r.confidence, .5), r.status, r.createdAt, r.lastAccessedAt, r.accessCount, r.sourceSessionId ?? null, r.sourceRound ?? null, r.sourceSeq ?? null, null, r.outcome ?? null, imageryToJson(r.imagery), r.slot?.room ?? null, r.slot?.index ?? null, r.imageryScore ?? null, review?.nextReviewAt ?? null, review?.easeFactor ?? null, review?.intervalDays ?? null, review?.reps ?? null);
|
|
13078
13379
|
sqlFtsInsert.run(r.id, tokenizeForFts(c.content));
|
|
13079
13380
|
}
|
|
13080
13381
|
for (const edge of data.edges ?? []) {
|
|
@@ -13121,12 +13422,20 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13121
13422
|
},
|
|
13122
13423
|
async attachEmbeddings(entries) {
|
|
13123
13424
|
let attached = 0;
|
|
13124
|
-
|
|
13125
|
-
|
|
13126
|
-
const
|
|
13127
|
-
|
|
13128
|
-
|
|
13129
|
-
|
|
13425
|
+
try {
|
|
13426
|
+
withTransaction(() => {
|
|
13427
|
+
for (const entry of entries) {
|
|
13428
|
+
guardDimension(entry.embedding);
|
|
13429
|
+
const result = sqlImportAttachEmbedding.run(vecToBlob(entry.embedding), entry.id);
|
|
13430
|
+
attached += Number(result.changes);
|
|
13431
|
+
}
|
|
13432
|
+
});
|
|
13433
|
+
} catch (error) {
|
|
13434
|
+
storedDim = computeStoredDim();
|
|
13435
|
+
throw error;
|
|
13436
|
+
}
|
|
13437
|
+
storedDim = computeStoredDim();
|
|
13438
|
+
bumpVectorMatrix();
|
|
13130
13439
|
return attached;
|
|
13131
13440
|
},
|
|
13132
13441
|
async rewriteVectors(targets) {
|
|
@@ -13138,6 +13447,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13138
13447
|
}
|
|
13139
13448
|
});
|
|
13140
13449
|
storedDim = computeStoredDim();
|
|
13450
|
+
bumpVectorMatrix();
|
|
13141
13451
|
return written;
|
|
13142
13452
|
},
|
|
13143
13453
|
async decay(options) {
|
|
@@ -13148,26 +13458,67 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13148
13458
|
return changed;
|
|
13149
13459
|
},
|
|
13150
13460
|
async findContradictions(embedding, limit = 3) {
|
|
13151
|
-
|
|
13461
|
+
const matrix = vectorMatrix();
|
|
13462
|
+
if (matrix === void 0) return [];
|
|
13463
|
+
if (matrix === "overflow") return db.prepare(`SELECT * FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0 AND scope IN ('user','project')`).all().map((row) => ({
|
|
13152
13464
|
row,
|
|
13153
13465
|
sim: cosine(embedding, blobToVec(row.embedding))
|
|
13154
13466
|
})).filter((entry) => entry.sim >= CONTRADICTION_COSINE).sort((a, b) => b.sim - a.sim).slice(0, limit).map((entry) => rowToRecord(entry.row));
|
|
13467
|
+
if (embedding.length !== matrix.dim) throw new EngramError("EMBEDDER_DIMENSION_MISMATCH", message("store.embedderDimensionMismatch", {
|
|
13468
|
+
expected: matrix.dim,
|
|
13469
|
+
found: embedding.length
|
|
13470
|
+
}));
|
|
13471
|
+
const queryUnit = unit(embedding);
|
|
13472
|
+
const hits = [];
|
|
13473
|
+
for (let i = 0; i < matrix.ids.length; i += 1) {
|
|
13474
|
+
if (matrix.scopeIdx[i] > 1) continue;
|
|
13475
|
+
const sim = dotAt(matrix, i, queryUnit);
|
|
13476
|
+
if (sim >= CONTRADICTION_COSINE) hits.push({
|
|
13477
|
+
index: i,
|
|
13478
|
+
sim
|
|
13479
|
+
});
|
|
13480
|
+
}
|
|
13481
|
+
hits.sort((a, b) => b.sim - a.sim);
|
|
13482
|
+
return hits.slice(0, limit).map((hit) => getRow(matrix.ids[hit.index])).filter((row) => row !== void 0).map((row) => rowToRecord(row));
|
|
13155
13483
|
},
|
|
13156
13484
|
async linkEdge(from, to, type) {
|
|
13157
13485
|
sqlEdgeUpsert.run(from, to, type, Date.now());
|
|
13158
13486
|
},
|
|
13159
13487
|
async nearestNeighbor(embedding) {
|
|
13160
|
-
const
|
|
13488
|
+
const matrix = vectorMatrix();
|
|
13489
|
+
if (matrix === void 0) return void 0;
|
|
13490
|
+
if (matrix === "overflow") {
|
|
13491
|
+
const pool = db.prepare(`SELECT * FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0 AND scope IN ('user','project')`).all();
|
|
13492
|
+
let best;
|
|
13493
|
+
for (const row of pool) {
|
|
13494
|
+
const sim = cosine(embedding, blobToVec(row.embedding));
|
|
13495
|
+
if (best === void 0 || sim > best.sim) best = {
|
|
13496
|
+
row,
|
|
13497
|
+
sim
|
|
13498
|
+
};
|
|
13499
|
+
}
|
|
13500
|
+
return best === void 0 ? void 0 : {
|
|
13501
|
+
record: rowToRecord(best.row),
|
|
13502
|
+
similarity: best.sim
|
|
13503
|
+
};
|
|
13504
|
+
}
|
|
13505
|
+
if (embedding.length !== matrix.dim) throw new EngramError("EMBEDDER_DIMENSION_MISMATCH", message("store.embedderDimensionMismatch", {
|
|
13506
|
+
expected: matrix.dim,
|
|
13507
|
+
found: embedding.length
|
|
13508
|
+
}));
|
|
13509
|
+
const queryUnit = unit(embedding);
|
|
13161
13510
|
let best;
|
|
13162
|
-
for (
|
|
13163
|
-
|
|
13511
|
+
for (let i = 0; i < matrix.ids.length; i += 1) {
|
|
13512
|
+
if (matrix.scopeIdx[i] > 1) continue;
|
|
13513
|
+
const sim = dotAt(matrix, i, queryUnit);
|
|
13164
13514
|
if (best === void 0 || sim > best.sim) best = {
|
|
13165
|
-
|
|
13515
|
+
index: i,
|
|
13166
13516
|
sim
|
|
13167
13517
|
};
|
|
13168
13518
|
}
|
|
13169
|
-
|
|
13170
|
-
|
|
13519
|
+
const row = best === void 0 ? void 0 : getRow(matrix.ids[best.index]);
|
|
13520
|
+
return row === void 0 || best === void 0 ? void 0 : {
|
|
13521
|
+
record: rowToRecord(row),
|
|
13171
13522
|
similarity: best.sim
|
|
13172
13523
|
};
|
|
13173
13524
|
},
|
|
@@ -13463,6 +13814,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13463
13814
|
sqlPurgeFacts.run();
|
|
13464
13815
|
sqlPurgeObservations.run();
|
|
13465
13816
|
});
|
|
13817
|
+
bumpVectorMatrix();
|
|
13466
13818
|
},
|
|
13467
13819
|
async close() {
|
|
13468
13820
|
db.close();
|
|
@@ -13859,7 +14211,7 @@ function assessEvidence(batch, request) {
|
|
|
13859
14211
|
//#endregion
|
|
13860
14212
|
//#region src/tools/create.ts
|
|
13861
14213
|
/**
|
|
13862
|
-
*
|
|
14214
|
+
* 23 个 engram_ 工具的定义与执行器。工具 schema 保持窄参数;
|
|
13863
14215
|
* scope 决定读写哪个分库;嵌入缺失时检索结果显式标记降级。
|
|
13864
14216
|
* @module @kenz1117/dsh-engram/tools/create
|
|
13865
14217
|
*/
|
|
@@ -14091,8 +14443,8 @@ async function rewriteQueries(deps, exec, query) {
|
|
|
14091
14443
|
}
|
|
14092
14444
|
}
|
|
14093
14445
|
/**
|
|
14094
|
-
* 构造
|
|
14095
|
-
* stats/export/import/distill/examine/neighbors/audit_forgotten/
|
|
14446
|
+
* 构造 23 个工具定义(engram_save/search/facts/assess/timeline/episode_timeline/update/forget/report/review/review_queue/
|
|
14447
|
+
* stats/export/import/distill/reflect/examine/neighbors/tour/audit_forgotten/ingest_history/profile_edit/language_guide)。
|
|
14096
14448
|
* @param baseDeps - 分库打开器、嵌入器、辅助 LLM、导出目录。
|
|
14097
14449
|
* @returns 可直接 register 的工具定义数组(execute 已绑定会话 cwd 的项目宫殿路由)。
|
|
14098
14450
|
*/
|
|
@@ -14651,7 +15003,7 @@ function createEngramTools(baseDeps) {
|
|
|
14651
15003
|
const input = args;
|
|
14652
15004
|
const scopes = scopesOf(input.scope);
|
|
14653
15005
|
const rooms = typeof input.room === "string" && input.room.trim() !== "" ? [resolveRoomInput(input.room) ?? input.room.trim()] : void 0;
|
|
14654
|
-
const limit = input.limit ?? 8;
|
|
15006
|
+
const limit = parseCountParam(input.limit ?? void 0, "engram_search", "limit", 8, 1, 50);
|
|
14655
15007
|
const asOfMs = parseTimeParam(typeof input.asOf === "string" ? input.asOf : void 0, "engram_search", "asOf");
|
|
14656
15008
|
const rewrite = await rewriteQueries(deps, exec, input.query);
|
|
14657
15009
|
const retrievals = await Promise.all(rewrite.queries.map(async (queryText) => {
|
|
@@ -16800,12 +17152,12 @@ function estimateTokens(text) {
|
|
|
16800
17152
|
}
|
|
16801
17153
|
//#endregion
|
|
16802
17154
|
//#region package.json
|
|
16803
|
-
var version = "0.11.
|
|
17155
|
+
var version = "0.11.1";
|
|
16804
17156
|
//#endregion
|
|
16805
17157
|
//#region src/index.ts
|
|
16806
17158
|
/**
|
|
16807
17159
|
* dsh-engram:DeepSeek Harness 跨会话长期记忆插件(host 半)。
|
|
16808
|
-
* 注册
|
|
17160
|
+
* 注册 23 个 engram_ 工具、会话开始注入画像(curated block 优先于派生画像)、
|
|
16809
17161
|
* 自动摄取上一轮对话、蒸馏/衰减飞轮与审计能力。
|
|
16810
17162
|
* @module @kenz1117/dsh-engram
|
|
16811
17163
|
*/
|
|
@@ -17274,7 +17626,7 @@ function persistPalaceLanguage(ctx, language) {
|
|
|
17274
17626
|
}, "dsh-engram: palace language persistence");
|
|
17275
17627
|
}
|
|
17276
17628
|
/**
|
|
17277
|
-
* 插件体:预热分库与嵌入器,注册
|
|
17629
|
+
* 插件体:预热分库与嵌入器,注册 23 个工具、画像注入、自动摄取与衰减调度。
|
|
17278
17630
|
* @param ctx - host 上下文。
|
|
17279
17631
|
* @param config - cordis.yml 传入的可选配置;非法值在加载时 loud 失败。
|
|
17280
17632
|
*/
|
|
@@ -17297,7 +17649,7 @@ async function apply(ctx, config = {}) {
|
|
|
17297
17649
|
language,
|
|
17298
17650
|
languageSetting
|
|
17299
17651
|
};
|
|
17300
|
-
if (determination
|
|
17652
|
+
if (determination?.decision.grounded) {
|
|
17301
17653
|
const decided = determination.decision.language;
|
|
17302
17654
|
ctx.effect(() => {
|
|
17303
17655
|
const timer = setTimeout(() => {
|
|
@@ -17330,7 +17682,9 @@ async function apply(ctx, config = {}) {
|
|
|
17330
17682
|
count: panelDictionary.problems.length,
|
|
17331
17683
|
sample: describeDictionaryProblems(panelDictionary.problems)
|
|
17332
17684
|
}));
|
|
17333
|
-
const
|
|
17685
|
+
const storeLanguage = resolved.embedder.remote === void 0 && (resolved.languageSetting === void 0 || resolved.languageSetting === "auto") ? await readStoreLanguageLabel(join(resolved.dbDir, "user.db")) : void 0;
|
|
17686
|
+
const mountLanguage = storeLanguage !== void 0 && registry.bindings.has(storeLanguage) ? storeLanguage : resolved.language;
|
|
17687
|
+
const localModel = resolveLocalModel(registry, mountLanguage, resolved.embedder.localModel, resolved.embedder.localPooling);
|
|
17334
17688
|
/** Снимок обслуживания по прочитанным словарям: собирается заново после правки из панели. */
|
|
17335
17689
|
/**
|
|
17336
17690
|
* Язык дворца в снимке обслуживания. Здесь всегда null: язык диалога появляется
|
|
@@ -17408,7 +17762,7 @@ async function apply(ctx, config = {}) {
|
|
|
17408
17762
|
* базы подписало и пересчитало под старый — и человек получил бы ровно то расхождение,
|
|
17409
17763
|
* от которого условие защищает.
|
|
17410
17764
|
*/
|
|
17411
|
-
let basesLanguage =
|
|
17765
|
+
let basesLanguage = mountLanguage;
|
|
17412
17766
|
/**
|
|
17413
17767
|
* Предохранитель смены языка: одно окно на процесс, состояние — в памяти.
|
|
17414
17768
|
*
|
|
@@ -17537,11 +17891,29 @@ async function apply(ctx, config = {}) {
|
|
|
17537
17891
|
* 面板「项目」scope、工具读写与自动摄取的 project 落点因此同一口径。
|
|
17538
17892
|
*/
|
|
17539
17893
|
const resolveProjectStore = (cwd) => cwd === void 0 ? openStore("project") : openStoreForProjectCwd(cwd);
|
|
17540
|
-
const
|
|
17541
|
-
|
|
17542
|
-
|
|
17894
|
+
const EMBEDDER_AWAIT_GRACE_MS = 15e3;
|
|
17895
|
+
let embedderPending;
|
|
17896
|
+
const embedderAttempt = () => {
|
|
17897
|
+
if (embedderPending === void 0) embedderPending = createEmbedder(resolved, registry, mountLanguage).catch((error) => {
|
|
17898
|
+
console.warn(message("core.warnEmbedderUnavailable"), error);
|
|
17899
|
+
embedderPending = void 0;
|
|
17900
|
+
});
|
|
17901
|
+
return embedderPending;
|
|
17902
|
+
};
|
|
17903
|
+
const embedder = { then(onfulfilled, onrejected) {
|
|
17904
|
+
const attempt = embedderAttempt();
|
|
17905
|
+
return Promise.race([attempt, new Promise((resolve) => {
|
|
17906
|
+
const timer = setTimeout(() => {
|
|
17907
|
+
resolve(void 0);
|
|
17908
|
+
}, EMBEDDER_AWAIT_GRACE_MS);
|
|
17909
|
+
attempt.then(() => {
|
|
17910
|
+
clearTimeout(timer);
|
|
17911
|
+
});
|
|
17912
|
+
})]).then(onfulfilled, onrejected);
|
|
17913
|
+
} };
|
|
17914
|
+
embedderAttempt();
|
|
17543
17915
|
ctx.effect(() => () => {
|
|
17544
|
-
|
|
17916
|
+
embedderPending?.then((active) => active?.close()).catch(() => {});
|
|
17545
17917
|
}, "dsh-engram: close embedder");
|
|
17546
17918
|
const logIngestRequest = (data) => {
|
|
17547
17919
|
openStore("user").then((store) => store.audit("ingest-request", "AUX", JSON.stringify(data))).catch(() => {});
|
|
@@ -17816,7 +18188,7 @@ async function apply(ctx, config = {}) {
|
|
|
17816
18188
|
};
|
|
17817
18189
|
const dimensions = baseTargetNow().model?.dimensions;
|
|
17818
18190
|
if (dimensions === void 0) return result;
|
|
17819
|
-
const active = await
|
|
18191
|
+
const active = await embedderAttempt();
|
|
17820
18192
|
if (active === void 0) {
|
|
17821
18193
|
result.failed.push({
|
|
17822
18194
|
file: "*",
|
|
@@ -18308,7 +18680,7 @@ async function apply(ctx, config = {}) {
|
|
|
18308
18680
|
if (route === void 0) continue;
|
|
18309
18681
|
const outcome = await reflectObservations({
|
|
18310
18682
|
store,
|
|
18311
|
-
embedder: await
|
|
18683
|
+
embedder: await embedderAttempt(),
|
|
18312
18684
|
scope: target.scope,
|
|
18313
18685
|
call: (params) => streamText(ctx, {
|
|
18314
18686
|
...params,
|
|
@@ -18332,6 +18704,7 @@ async function apply(ctx, config = {}) {
|
|
|
18332
18704
|
}
|
|
18333
18705
|
})();
|
|
18334
18706
|
});
|
|
18707
|
+
const OP_LOG_RETENTION_DAYS = 366;
|
|
18335
18708
|
const runDecay = async () => {
|
|
18336
18709
|
const targets = [{
|
|
18337
18710
|
label: "user",
|
|
@@ -18344,8 +18717,10 @@ async function apply(ctx, config = {}) {
|
|
|
18344
18717
|
open: () => openDb(palace.dbName)
|
|
18345
18718
|
});
|
|
18346
18719
|
}
|
|
18720
|
+
const pruneBefore = Date.now() - OP_LOG_RETENTION_DAYS * 864e5;
|
|
18347
18721
|
for (const target of targets) {
|
|
18348
|
-
const
|
|
18722
|
+
const store = await target.open();
|
|
18723
|
+
const archived = await store.decay({
|
|
18349
18724
|
importanceBelow: resolved.decayImportanceBelow,
|
|
18350
18725
|
olderThanDays: resolved.decayAfterDays
|
|
18351
18726
|
});
|
|
@@ -18353,6 +18728,11 @@ async function apply(ctx, config = {}) {
|
|
|
18353
18728
|
label: target.label,
|
|
18354
18729
|
count: archived
|
|
18355
18730
|
}));
|
|
18731
|
+
try {
|
|
18732
|
+
await store.pruneOps(pruneBefore, LOG_KEEP_OPS);
|
|
18733
|
+
} catch (error) {
|
|
18734
|
+
console.warn(message("core.warnDecayPeriodicFailed"), error);
|
|
18735
|
+
}
|
|
18356
18736
|
}
|
|
18357
18737
|
};
|
|
18358
18738
|
runDecay().catch((error) => {
|